Professional Documents
Culture Documents
SAMPLE RUN
Step-by-step sample run of your assembly program with explanation.
YouTube link: https://youtu.be/xsenc9iGg0A
Type nasm -f win32 filename.asm to
assemble it into obj file
ASSEMBLERS INSTRUCTIONS
Selection Statements Iterative Statements
1. `cmp` (Compare): 1. `.menu_loop` Label:
- Purpose: The `cmp` instruction is used - Purpose: This label defines the
to compare two values. It sets the flags in starting point of a loop that repeatedly
the EFLAGS register according to the displays the menu options and awaits the
result of the comparison, such as whether user's choice until the user selects the
the values are equal, greater, or less. "EXIT" option. It effectively serves as a
- Examples in the code: loop that keeps the program running and
- `cmp eax, 1`: Compares the value in offering menu choices.
the `eax` register with the constant value
1 to check if the user's choice is equal to 2. `jmp .menu_loop`:
1 (addition). - Purpose: This instruction is a jump
- Similar `cmp` instructions are used (unconditional) that transfers control back
for other menu options to check the to the `.menu_loop` label, effectively
PROGRAM CODE
section .data
; Welcome and information messages
prompt_welcome db 'Hi! This is Jom and Im here to help you perform',10,0
prompt_display db 'WELCOME TO JOM`S CALCULATOR',10, 0
; Menu options
prompt_exit db '[0] EXIT',10, 0
prompt_add db '[1] ADDITION',10, 0
prompt_sub db '[2] SUBTRACTION',10, 0
prompt_mul db '[3] MULTIPLICATION',10, 0
prompt_div db '[4] DIVISION',10, 0
selectChoice db 'PLEASE ENTER YOUR DESIRED OPERATION:', 0
inputformat_selectChoice db '%d', 0
; Result messages
result_sum db 'Sum: %d',10, 0
result_diff db 'Difference: %d',10, 0
result_prod db 'Product: %d',10, 0
result_quotient db 'Quotient: %d',10, 0
result_remainder db 'Remainder: %d',10, 0
prompt_div_zero db 'OPERATION CAN`T BE PERFORMED',10, 0 ; Error message
; Section headers
prompt_addition db ' ADDITION ', 10, 0
prompt_subtraction db ' SUBTRACTION ', 10, 0
prompt_multiplication db ' MULTIPLICATION ', 10, 0
prompt_division db ' DIVISION ', 10, 0
section .bss
menu_choice resb 100
first_num resd 1
second_num resd 1
section .text
global _main
extern _atoi
extern _printf
extern _scanf
extern _exit
_main:
; display welcome message
push prompt_welcome
call _printf
add esp, 8
.menu_loop:
; menu options
push prompt_exit
call _printf
add esp, 4
push prompt_add
call _printf
add esp, 4
push prompt_sub
call _printf
add esp, 4
push prompt_mul
call _printf
add esp, 4
push prompt_div
call _printf
add esp, 4
push selectChoice
call _printf
add esp, 4
push menu_choice
push inputformat_selectChoice
call _scanf
add esp, 8
.exit:
call _exit
.addition:
; addition options
push prompt_addition
call _printf
add esp, 4
push prompt_first_num
call _printf
add esp, 4
push first_num
push inputformat_numbers
call _scanf
add esp, 8
push prompt_second_num
call _printf
add esp, 4
push second_num
push inputformat_numbers
call _scanf
add esp, 8
jmp .menu_loop
.subtraction:
; subtraction options
push prompt_subtraction
call _printf
add esp, 4
push prompt_first_num
call _printf
add esp, 4
push first_num
push inputformat_numbers
call _scanf
add esp, 8
push prompt_second_num
call _printf
add esp, 4
push second_num
push inputformat_numbers
call _scanf
add esp, 8
jmp .menu_loop
.multiplication:
; multiplication options
push prompt_multiplication
call _printf
add esp, 4
push prompt_first_num
call _printf
add esp, 4
push first_num
push inputformat_numbers
call _scanf
add esp, 8
push prompt_second_num
call _printf
add esp, 4
push second_num
push inputformat_numbers
call _scanf
add esp, 8
jmp .menu_loop
.division:
; division options
push prompt_division
call _printf
add esp, 4
push prompt_first_num
call _printf
add esp, 4
push first_num
push inputformat_numbers
call _scanf
add esp, 8
push prompt_second_num
call _printf
add esp, 4
push second_num
push inputformat_numbers
call _scanf
add esp, 8
jmp .menu_loop
.division_zero:
; display error message
push prompt_div_zero
call _printf
add esp, 4
push prompt_second_num
call _printf
add esp, 4
push second_num
push inputformat_numbers
call _scanf
add esp, 8
jmp .menu_loop
Republic of the Philippines
CAMARINES SUR POLYTECHNIC COLLEGES
Nabua, Camarines Sur