hw4_ex1.asm

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

; The program prompts the user to enter 2 positive integers. The program aborts
; with an error message if either number is not encodable as a 1-byte value.
; The program then computes the sum of the two integers. If there is an
; overflow, the program prints an error message. Otherwise, the program prints
; the result.

%include "asm_io.inc"

segment .data
        prompt          db      "Enter a 1-byte unsigned number: ", 0
        invalid         db      "Invalid number...", 0
        overflow        db      "Overfow!", 0
        sum             db      "The summation of these two numbers is: ", 0

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

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

        mov     eax, prompt    ; print out the prompt message
        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, 255       ; compare the input with 255
        jnbe    else_block     ; branch if the input is larger than 255

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

        mov     eax, prompt    ; print out the prompt message
        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 a negative number
        cmp     eax, 255       ; cmpare the input with 255
        jnbe    else_block     ; branch if the input is larger than 255

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

        mov     bl, [input1]   ; mov "input1" into bl
        add     bl, [input2]   ; add bl with "input2", store the result in bl
        jc      overflow_block ; jump if carray bit is set

        mov     eax, sum       ; print out the summation message
        call    print_string
        movzx   eax, bl        ; unsigned extend bl into eax
        call    print_int      ; print out the result
        call    print_nl
        jmp     end            ; skip the overflow_block and else_block

else_block:
        mov     eax, invalid   ; print out the number invalid message
        call    print_string
        call    print_nl
        jmp     end            ; skip the overflow_block

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

end:

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