hw5_ex2.asm

; Jade Yu Cheng
; ICS 312
; Assignment 5 Exercise 2
; 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. The program computes a tally for each possible
; number.  For instance, if the user has entered the number "3" 4 times, then
; the tally for number "3" is 4. Instead of merely printing the entered number,
; the program prints the tallies in order, with an appropriately readable format.

%include "asm_io.inc"

segment .data
        prompt          db      "Enter a number between 0 and 9: ", 0     ; prompt msg
        tally           db      "The tally for ", 0             ; tally msg 1
        is              db      " is: ", 0                      ; tally msg 2
        invalid         db      "Invalid number!", 0      ; invalid msg
        t               db      0, 0, 0, 0, 0, 0, 0, 0, 0, 0    ; counters

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

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

; This block of code prompts the user input in a loop and store them in n.
        mov     edx, 0          ; initialize edx = 0
        mov     ebx, n          ; initialize ebx to point to the starting of n
loop_input_start:
        cmp     edx, 9          ; compare edx with 9
        jg      loop_input_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    near else_block ; branch if the input is a negative number
        cmp     eax, 9          ; compare the input with 9
        jnbe    near else_block ; branch if the input is larger than 9
        mov     [ebx], al       ; store the number as one byte quantity in ebx

        inc     edx             ; increment loop count edx
        inc     ebx             ; increment ebx to point to the next position
        jmp     loop_input_start
loop_input_end:

; This block of code counts the occurences of inputs and store them in t.
        mov     dl, 0           ; initialize edx = 0
        mov     ebx, n          ; initialize ebx to point to the starting of n
loop_count_start:
        cmp     dl, 9           ; compare dl with 9
        jg      near loop_count_end

        mov     ecx, t          ; let ecx points to the beginning of t
        movzx   eax, byte [ebx] ; extend item as a byte into eax
        add     ecx, eax        ; move ecx ptr to a corresponding position in t
        inc     byte [ecx]      ; increment the corresponding counter in t

        inc     ebx             ; increment ebx to point ot the next position
        inc     edx             ; increment edx
        jmp     loop_count_start
loop_count_end:

; This block of code prints out the contents of t in a formatted fashion.
        mov     edx, 0          ; initialize edx to be 0
        mov     ebx, t          ; initialize ebx to point to t
loop_print_start:
        cmp     edx, 9          ; compare edx with 9
        jg      end             ; terminate the loop if 10 iterations are done

        mov     eax, tally      ; print out the first half of the message
        call    print_string
        mov     eax, edx        ; print out the current iterations number
        call    print_int
        mov     eax, is         ; print out the second half of the message
        call    print_string
        mov     al, [ebx]       ; put the value in the address of ebx into al
        movzx   eax, al         ; unsigned extend al to eax
        call    print_int       ; print out the value
        call    print_nl
        inc     edx             ; increment edx
        inc     ebx             ; increment ebx to the next value in t
        jmp     loop_print_start

; This block of code prints out invalid input message and jump back to the
; user input loop for more inputs.
else_block:
        mov     eax, invalid    ; print out the number invalid message
        call    print_string
        call    print_nl
        jmp     loop_input_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