hw4_ex3.asm

; Jade Yu Cheng
; ICS 312
; Assignment 4 Exercise 3
; Feb 28, 2009

; This program is extended from hw4_ex2.asm.  Program supports signed number.

%include "asm_io.inc"

segment .data
        number_prompt      db           "Enter a 1-byte unsigned number: ", 0
        invalid_number     db           "Invalid number...", 0
        overflow           db           "Overfow!", 0
        operation_prompt   db           "Enter an operation: ", 0
        invalid_operation  db           "Invalid operation...", 0

segment .bss
        input1    resb     1            ; reserve space for the first number
        input2    resb     1            ; reserve space for the second number

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

        mov     eax, number_prompt      ; print out the number prompt message
        call    print_string
        call    read_int                ; read a number and store it in eax

        cmp     eax, -128               ; compare the input with -128
        jnge    else_block              ; branch if the input is too small
        cmp     eax, 127                ; compare the input with 127
        jnle    else_block              ; branch if the input is too large

        mov     [input1], eax           ; if the input is in range,
                                        ; store it in "input1
        mov     eax, number_prompt      ; print out the number prompt message
        call    print_string
        call    read_int                ; read a number and store it in eax
        cmp     eax, -128               ; compare the input with 0
        jnge    else_block              ; branch if the input is too small
        cmp     eax, 127                ; cmpare the input with 127
        jnle    else_block              ; branch if the input is too large

        mov     [input2], eax           ; if the input is in range,
                                        ; store it in "input2"

        call    read_char               ; absorb the enter key stroke
        mov     eax, operation_prompt   ; print out the opeerator prompt message
        call    print_string
        call    read_char               ; read the charactor, store it in eax

        cmp     eax, 43                 ; compare the charactor with '+'
        je      add_block               ; branch if the charactor is '+'
        cmp     eax, 42                 ; compare the charactor with '*'
        je      mul_block               ; branch if the charactor is '*'

        mov     eax, invalid_operation  ; print out the operation error message
        call    print_string
        call    print_nl
        jmp     end                     ; jump to the end of the program

else_block:
        mov     eax, invalid_number     ; print out the number invalid message
        call    print_string
        call    print_nl
        jmp     end                     ; jump to the end of the prgram

add_block:
        mov     bl, [input1]            ; move "input1" into bl
        add     bl, [input2]            ; add bl with "input2" and
                                        ; store the result in bl
        jo      overflow_block          ; jump if overflow bit is set
        jmp     result_block            ; jump to the result_block

mul_block:
        mov     al, [input1]            ; move "input1" into al
        imul    byte [input2]           ; multiply al with "input2" as a byte
                                        ; size number, store the result in al
        jo      overflow_block          ; jump is the overflow bit is set
        mov     bl, al                  ; move al to bl
        jmp     result_block            ; jump to the result_block

overflow_block:
        mov     eax, overflow           ; print out the overflow error message
        call    print_string
        call    print_nl
        jmp     end

result_block:
        movsx   eax, bl                 ; unsigned extend bl into eax
        call    print_int               ; print out the result
        call    print_nl

end:

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