You are on page 1of 17

Republic of the Philippines

CAMARINES SUR POLYTECHNIC COLLEGES


Nabua, Camarines Sur

COLLEGE of COMPUTER STUDIES

CS 318 – Architecture and Organization


LEARNING TASK (STRUCTURED ASSEMBLY LANGUAGE P1)

NAME: Jomar P. Damot SECTION: BSCS 3B

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

Type gcc -o filename filename.obj to


assemble it into exe file
And type the filename only to run the exe
file

Now its working


Republic of the Philippines
CAMARINES SUR POLYTECHNIC COLLEGES
Nabua, Camarines Sur

COLLEGE of COMPUTER STUDIES

Try some actions

And close the application


Republic of the Philippines
CAMARINES SUR POLYTECHNIC COLLEGES
Nabua, Camarines Sur

COLLEGE of COMPUTER STUDIES

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

user's choice. creating a loop that keeps the menu


options displayed and waits for the user
2. `je ` (Jump If Equal): to make a choice.
- Purpose: The `je ` instruction is a
conditionaljump instruction that is used to 3. Conditional Jumps (`je `, `cmp`):
transfer control to a specified location (a - Purpose: These conditionaljump
label) if the previous comparison using instructions (e.g., `je `) are used within the
`cmp` resulted in equality (if the zero flag menu loop to navigate to different
is set in EFLAGS). sections of code based on the user's
- Examples in the code: choice. They allow the program to
- `je .addition`: If the previous `cmp` respond to the user's input and execute
instruction indicated that the user's choice the corresponding operation, or exit the
is equal to 1, the program jumps to the loop and terminate the program.
label `.addition`, which is the addition
operation section of the code.
- Similar `je ` instructions are used for
other menu options to jump to their
respective sections of the code based on
the user's choice.
Republic of the Philippines
CAMARINES SUR POLYTECHNIC COLLEGES
Nabua, Camarines Sur

COLLEGE of COMPUTER STUDIES

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

; User input prompts and formats


prompt_first_num db 'Enter 1st number:', 0
prompt_second_num db 'Enter 2nd number:', 0
inputformat_numbers 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

; display prompt message


push prompt_display
call _printf
add esp, 4

.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

mov eax, [menu_choice]


cmp eax, 1
je .addition
cmp eax, 2
je .subtraction
cmp eax, 3
je .multiplication
cmp eax, 4
je .division
cmp eax, 0
je .exit
jmp .menu_loop

.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

mov eax, [first_num]


add eax, [second_num]
push eax
push result_sum
call _printf
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

mov eax, [first_num]


sub eax, [second_num]
push eax
push result_diff
call _printf
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

mov eax, [first_num]


imul eax, [second_num]
push eax
push result_prod
call _printf
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

mov eax, [second_num]


cmp eax, 0
je .division_zero

mov eax, [first_num]


cdq
mov ebx, [second_num]
idiv ebx
push eax
push result_quotient
call _printf
add esp, 8

mov eax, dword [first_num]


add eax, dword [second_num]
mov edx, 0
div ebx
push edx
push result_remainder
call _printf
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

mov eax, [second_num]


cmp eax, 0
je .division_zero

mov eax, [first_num]


cdq
mov ebx, [second_num]
idiv ebx
push eax
push result_quotient
call _printf
add esp, 8

mov eax, dword [first_num]


add eax, dword [second_num]
mov edx, 0
div ebx
push edx
push result_remainder
call _printf
add esp, 8

jmp .menu_loop
Republic of the Philippines
CAMARINES SUR POLYTECHNIC COLLEGES
Nabua, Camarines Sur

COLLEGE of COMPUTER STUDIES


Republic of the Philippines
CAMARINES SUR POLYTECHNIC COLLEGES
Nabua, Camarines Sur

COLLEGE of COMPUTER STUDIES


Republic of the Philippines
CAMARINES SUR POLYTECHNIC COLLEGES
Nabua, Camarines Sur

COLLEGE of COMPUTER STUDIES


Republic of the Philippines
CAMARINES SUR POLYTECHNIC COLLEGES
Nabua, Camarines Sur

COLLEGE of COMPUTER STUDIES


Republic of the Philippines
CAMARINES SUR POLYTECHNIC COLLEGES
Nabua, Camarines Sur

COLLEGE of COMPUTER STUDIES


Republic of the Philippines
CAMARINES SUR POLYTECHNIC COLLEGES
Nabua, Camarines Sur

COLLEGE of COMPUTER STUDIES


Republic of the Philippines
CAMARINES SUR POLYTECHNIC COLLEGES
Nabua, Camarines Sur

COLLEGE of COMPUTER STUDIES

You might also like