; Jade Yu Cheng ; ICS 312 ; Assignment 4 Exercise 2 ; Feb 28, 2009 ; This program is extended from hw4_ex1.asm. On top of the functionalities of ; hw4_ex1.asm, program asks the user whether an addition or a multiplication is ; needed (still using only 1-byte operations on unsigned quantities). The user ; is prompted for a character, which can be '+' or '*', and the program prints ; an error message if an invalid operation is entered. %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 result resb 1 ; reserve space for the result 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, 0 ; compare the input with 0 jnge else_block ; branch if the input is negative cmp eax, 255 ; compare the input with 255 jnbe 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, 0 ; compare the input with 0 jnge else_block ; branch if the input is negative cmp eax, 255 ; cmpare the input with 255 jnbe 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 operator 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 jc overflow_block ; jump if carray bit is set jmp result_block ; jump to the result_block mul_block: mov al, [input1] ; move "input1" into al mul byte [input2] ; multiply al with "input2" as a byte ; size number, store the result in al jc overflow_block ; jump is the carray 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: mov eax, result ; print out the result message call print_string movzx 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