hw5_ex1.asm

; Jade Yu Cheng
; ICS 312
; Assignment 5 Exercise 1
; March 17, 2009

; The program prompt the user to enter 10 numbers between 0 and 9 (both
; included). The program prints an error message and re-prompt the user for
; another number if the number entered is invalid. These numbers are stored in
; memory as 1-byte values. Once the user has entered 10 valid numbers, the
; program prints them out in the order they were entered.

%include "asm_io.inc"

segment .data
        prompt          db      "Enter a number between 0 and 9: ", 0     ; a message
        invalid         db      "Invalid number!", 0      ; a message

segment .bss
        n       resb    10      ; reserve space for the numbers

segment .text
        global asm_main
asm_main:
        enter   0,0             ; setup
        pusha                   ; setup

        mov     edx, 0          ; initialize edx = 0
        mov     ebx, n          ; initialize ebx to point to the starting of n
loop_start:
        cmp     edx, 9          ; compare edx with 9
        jg      loop_end        ; terminate the loop if 10 iterations are done

        mov     eax, prompt     ; print out the message to prompt user input
        call    print_string
        call    read_int        ; read a number and store it in eax
        cmp     eax, 0          ; compare the input with 0
        jnge    else_block      ; branch if the input is a negative number
        cmp     eax, 9          ; compare the input with 9
        jnbe    else_block      ; branch if the input is larger than 9

        inc     edx             ; increment edx
        inc     ebx             ; increment ebx to point to the next position
        mov     [ebx], al       ; store the number as a one byte quantity

        jmp     loop_start
loop_end:

        mov     edx, 0          ; initialize edx = 0
        mov     ebx, n          ; initialize ebx to point to the starting of n
loop_print_start:
        cmp     edx, 9          ; compare edx with 9
        jg      end             ; terminate the loop if 10 iterations are done
        inc     edx             ; increment edx
        inc     ebx             ; increment ebx to point ot the next position
        mov     eax, 0          ; wipe out garbage in eax
        mov     al, [ebx]       ; put the contents of the address that ebx
                                ; points to input eax as a one byte quantity
        call    print_int       ; print out the content of eax
        call    print_nl
        jmp     loop_print_start

else_block:
        mov     eax, invalid    ; print out the number invalid message
        call    print_string
        call    print_nl
        jmp     loop_start      ; go back to the loop for more inputs
end:

        popa                    ; cleanup
        mov     eax, 0          ; cleanup
        leave                   ; cleanup
        ret                     ; cleanup
Valid HTML 4.01 Valid CSS