Skip to content
Snippets Groups Projects
Commit 58ef79ae authored by k26-chow's avatar k26-chow
Browse files

Delete task2.asm

parent aef46c25
No related branches found
No related tags found
No related merge requests found
int __attribute__((cdecl)) asm_main(void);
int main() {
int ret_status;
ret_status = asm_main();
return ret_status;
}
nasm -f elf task2.asm -o task2.o
nasm -f elf asm_io.asm -o asm_io.o
gcc -m32 -c driver.c -o driver.o
gcc -m32 driver.o task2.o asm_io.o -o task2
%include "asm_io.inc" ; Include I/O functions (like print_string, read_int, read_string)
segment .data
prompt_name db "Enter your name: ", 0 ; Prompt for name
prompt_number db "Enter a number between 50 and 100: ", 0 ; Prompt for number
welcome_msg db "Welcome, ", 0 ; Welcome message
newline db 10, 0 ; Newline character
error_msg db "Error: The number must be between 50 and 100!", 0 ; Error message
segment .bss
user_name resb 50 ; Reserve 50 bytes for the user's name
user_input resd 1 ; Reserve space for user input (the number)
section .text
global asm_main
asm_main:
pusha ; Save all registers
; Ask the user for their name
mov eax, prompt_name ; Load the prompt_name address into EAX
call print_string ; Print the name prompt
call read_string ; Read the user's name into user_name
; Ask the user for a number between 50 and 100
mov eax, prompt_number ; Load the prompt_number address into EAX
call print_string ; Print the number prompt
call read_int ; Read the user's input into EAX (the number)
mov [user_input], eax ; Store the user's input in user_input
; Check if the number is between 50 and 100
mov eax, [user_input] ; Load the user input into EAX
cmp eax, 50 ; Compare the number to 50
jle invalid_input ; Jump to invalid_input if number is <= 50
cmp eax, 100 ; Compare the number to 100
jge invalid_input ; Jump to invalid_input if number is >= 100
; If the number is valid, print the welcome message with the user's name
mov eax, welcome_msg ; Load the welcome message into EAX
call print_string ; Print the welcome message
mov eax, user_name ; Load the user's name into EAX
call print_string ; Print the user's name
call print_nl ; Print a newline
; Print the welcome message the number of times entered by the user
mov ecx, [user_input] ; Set ECX (loop counter) to the user input number
print_welcome:
mov eax, welcome_msg ; Load the welcome message into EAX
call print_string ; Print the welcome message
call print_nl ; Print a newline
loop print_welcome ; Loop until ECX (the number) is 0
jmp end_program ; Jump to end_program after printing the welcome messages
invalid_input:
; Print the error message if the input is invalid
mov eax, error_msg ; Load the error message into EAX
call print_string ; Print the error message
call print_nl ; Print a newline
jmp asm_main ; Ask for the input again
end_program:
popa ; Restore registers
mov eax, 0 ; Return 0 to indicate successful execution
ret
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment