Skip to content
Snippets Groups Projects
Commit 8993161c authored by Leo Trung (Student)'s avatar Leo Trung (Student)
Browse files

Worksheet 1 task 2 and 3 completion

parent 2961b898
Branches
No related tags found
No related merge requests found
all: task1 task2_1 task2_2 task2_3
task1: task1.asm
nasm -f elf task1.asm -o task1.o
nasm -f elf asm_io.asm -o asm_io.o
gcc -m32 -c driver.c -o driver.o
gcc -m32 driver.o task1.o asm_io.o -o task1
task2_1: task2_1.asm
nasm -f elf task2_1.asm -o task2_1.o
nasm -f elf asm_io.asm -o asm_io.o
gcc -m32 -c driver.c -o driver.o
gcc -m32 driver.o task2_1.o asm_io.o -o task2_1
task2_2: task2_2.asm
nasm -f elf task2_2.asm -o task2_2.o
nasm -f elf asm_io.asm -o asm_io.o
gcc -m32 -c driver.c -o driver.o
gcc -m32 driver.o task2_2.o asm_io.o -o task2_2
task2_3: task2_3.asm
nasm -f elf task2_3.asm -o task2_3.o
nasm -f elf asm_io.asm -o asm_io.o
gcc -m32 -c driver.c -o driver.o
gcc -m32 driver.o task2_3.o asm_io.o -o task2_3
\ No newline at end of file
%include "asm_io.inc"
section .data
prompt_name db "Enter your name: ", 0
prompt_times db "Enter the number of times (51-99) to print the welcome message: ", 0
invalid_input db "Invalid input! Please enter a number between 51 and 99.", 10, 0
welcome_msg db "Welcome, %s!", 10, 0
section .bss
name resb 50 ; Reserve space for the name (50 bytes)
times resd 1 ; Reserve space for the number (4 bytes)
section .text
extern asm_main ; Declared in driver.c
global asm_main
asm_main:
; Prompt for name
mov eax, prompt_name ; Load the address of the prompt string
call print_string ; Print the string
mov eax, name ; Load the address of the name buffer
mov ebx, 50 ; Set max input length
call read_string ; Read user input into the name buffer
ask_times:
; Prompt for the number of times
mov eax, prompt_times ; Load the address of the prompt string
call print_string ; Print the string
call read_int ; Read an integer from the user
mov [times], eax ; Store the input number in the `times` variable
; Validate the input (51 <= times <= 99)
mov eax, [times] ; Load the number into eax
cmp eax, 51 ; Check if less than 51
jl invalid_input_msg ; Jump if invalid
cmp eax, 99 ; Check if greater than 99
jg invalid_input_msg ; Jump if invalid
jmp print_messages ; Valid input, proceed
invalid_input_msg:
mov eax, invalid_input ; Load the invalid input message
call print_string ; Print the string
jmp ask_times ; Re-prompt the user
print_messages:
mov ecx, [times] ; Load the number of times into ecx (loop counter)
print_loop:
mov eax, welcome_msg ; Lo
No preview for this file type
segment .data segment .data
prompt_num db "Enter number of times to print the message: ", 0
prompt db "Enter your name: ", 0 prompt db "Enter your name: ", 0
error_prompt db "You have entered a number that does not satisfy the requirement", 0
format db "%s", 0 format db "%s", 0
format_digit db "%d", 0
welcome_msg db "Welcome, %s!", 0 welcome_msg db "Welcome, %s!", 0
newline db 10, 0 newline db 10, 0
number dd 0
count db 0
segment .bss segment .bss
name resb 50 ; Reserve space for the name (50 bytes) name resb 50 ; Reserve space for the name (50 bytes)
num resb 1
counter resb 1
segment .text segment .text
extern printf extern printf
...@@ -25,6 +28,34 @@ asm_main: ...@@ -25,6 +28,34 @@ asm_main:
call scanf call scanf
add esp, 8 ; Clean up the stack (2 arguments) add esp, 8 ; Clean up the stack (2 arguments)
;Print the number prompt
push prompt_num
call printf
add esp, 4
; Read the number input
push num
push format_digit
call scanf
add esp, 8
; Loop
mov al, [num] ; Load the value of 'num' into register AL
mov [counter], al ; Store it in the counter variable
; Check if num > 50
cmp al, 50 ; Compare num with 50
jle error_exit ; If num <= 50, jump to error_exit
; Check if num < 100
cmp al, 100 ; Compare num with 100
jge error_exit ; If num >= 100, jump to error_exit
loop_start:
; Do something here (inside the loop)
; Print the welcome message ; Print the welcome message
push name ; Push the name to print push name ; Push the name to print
push welcome_msg push welcome_msg
...@@ -36,8 +67,32 @@ asm_main: ...@@ -36,8 +67,32 @@ asm_main:
call printf call printf
add esp, 4 add esp, 4
; Decrement the counter
dec byte [counter] ; Decrement the value of 'counter'
; Check if the counter is zero
cmp byte [counter], 0 ; Compare counter with 0
jne loop_start ; If not zero, jump back to loop_start
; Exit the program ; Exit the program
mov eax, 1 ; syscall: exit mov eax, 1 ; syscall: exit
xor ebx, ebx ; exit code 0 xor ebx, ebx ; exit code 0
ret ret
int 0x80 int 0x80
error_exit:
; Exit with error code
push error_prompt
call printf
add esp, 4
;Print newline before exiting
push newline
call printf
add esp, 4
;Exit with error code
mov eax, 60 ; System call for exit
mov edi, 1 ; Exit code 1 (error)
syscall
\ No newline at end of file
File added
No preview for this file type
section .data
sum_prompt db "The sum of the numbers is: %d", 10, 0 ; Format string for sum
total db 0 ; Reserve space for the sum result
section .bss
sum resd 1 ; Reserve space for the sum (4 bytes)
section .text
extern printf
global asm_main
asm_main:
; Initialize registers
xor eax, eax ; Clear eax, we'll use it to store the sum
; Calculate sum of integers from 1 to 100
mov ecx, 1 ; Start loop counter at 1
mov ebx, 100 ; Upper limit of the loop
calculate_sum:
add eax, ecx ; Add the current counter value to the sum (in eax)
inc ecx ; Increment the counter
cmp ecx, ebx ; Compare counter with 100
jle calculate_sum ; Loop if counter is <= 100
; Now eax contains the sum of numbers from 1 to 100
; Store the sum in the variable sum
mov [sum], eax
; Print the sum using printf
push eax ; Push the sum onto the stack (argument for printf)
push sum_prompt ; Push the format string
call printf ; Call printf
add esp, 8 ; Clean up the stack (2 arguments)
; Exit the program
mov eax, 1 ; syscall: exit
xor ebx, ebx ; exit code 0
ret
int 0x80
File added
File added
section .data
sum_prompt db "The sum of the numbers is: %d", 10, 0 ; Format string for sum
prompt_start db "Enter the start of the range: ", 0
prompt_end_range db "Enter the end of the range: ", 0
format db "%d", 0
newline db 10, 0
total db 0 ; Reserve space for the sum result
section .bss
sum resd 1 ; Reserve space for the sum (4 bytes)
start_range resd 1
end_range resd 1
section .text
extern printf
extern scanf
global asm_main
asm_main:
; Print start prompt
push prompt_start
call printf
add esp, 4
; Scan start range
push start_range
push format
call scanf
add esp, 8
; Print end range prompt
push prompt_end_range
call printf
add esp, 4
; Scan end range
push end_range
push format
call scanf
add esp, 8
; Initialize sum register (eax = 0)
xor eax, eax ; Clear eax register to store the sum
; Load start and end range values into registers
mov ecx, [start_range] ; Start range (ecx)
mov ebx, [end_range] ; End range (ebx)
calculate_sum:
cmp ecx, ebx ; Compare start value (ecx) with end value (ebx)
jg done_calculation ; If start > end, exit the loop
add eax, ecx ; Add current value of ecx to eax (sum)
inc ecx ; Increment ecx to move to the next number
jmp calculate_sum ; Repeat the loop
done_calculation:
; Now eax contains the sum of numbers from start_range to end_range
; Store the sum in the sum variable (optional)
mov [sum], eax
; Print the sum using printf
push eax ; Push the sum onto the stack (argument for printf)
push sum_prompt ; Push the format string
call printf ; Call printf
add esp, 8 ; Clean up the stack (2 arguments)
; Exit the program
mov eax, 1 ; syscall: exit
xor ebx, ebx ; exit code 0
ret
int 0x80
File added
segment .data
prompt db "Enter your name: ", 0
format db "%s", 0
welcome_msg db "Welcome, %s!", 0
newline db 10, 0
segment .bss
name resb 50 ; Reserve space for the name (50 bytes)
segment .text
extern printf
extern scanf
global asm_main
asm_main:
; Print the prompt
push prompt
call printf
add esp, 4 ; Clean up the stack
; Read the input
push name ; Address of the buffer to store the input
push format ; Format string for scanf
call scanf
add esp, 8 ; Clean up the stack (2 arguments)
; Print the welcome message
push name ; Push the name to print
push welcome_msg
call printf
add esp, 8 ; Clean up the stack (2 arguments)
; Print newline
push newline
call printf
add esp, 4
; Exit the program
mov eax, 1 ; syscall: exit
xor ebx, ebx ; exit code 0
ret
int 0x80
\ No newline at end of file
File deleted
File deleted
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment