CSC231 generateRandomNumbers.asm

From dftwiki3
Jump to: navigation, search

--D. Thiebaut 19:42, 9 December 2008 (UTC)


;;; generateRandomNumbers.asm
;;; D. Thiebaut
;;;
;;; prints 10 random numbers that are less than 1024
;;;
;;; The driver.c program must be modified for this occasion as
;;; follows:
;;; 
;;; -----------------------------------------------------------
;;; #include <stdio.h>
;;; #include <stdlib.h>
;;; #include <time.h>
;;; 
;;; extern int asm_main( void )
;;; 
;;; 
;;; int main() {
;;;     srand( time( 0 ) )
;;;     asm_main()
;;; }
;;;
;;; -----------------------------------------------------------
;;; 
;;; To assemble, link and run:
;;;  nasm -f elf asm_io.asm
;;;  nasm -f elf xxxx.asm
;;;  gcc -o xxxx driver.c asm_io.o xxxx.o
;;;
;;; 
%include "asm_io.inc"

extern  rand                    ;C function returning a random int
        
        ;; -------------------------
        ;; data segment
        ;; -------------------------
        section .data

        ;; -------------------------
        ;; code area
        ;; -------------------------
        section .text
        global  asm_main
asm_main:       

;;; get the local time in year, month, day, hour, min, sec...
        mov     ecx, 10
for:    push    ecx
        call    rand            ;rand returns # in eax
        and     eax, 0x000003ff ;keep only last 10 bits
        call    print_int
        call    print_nl
        pop     ecx
        loop    for
        
        ;; return to C program

        ret