From 5743f1530ac3babf86798a8ac58abe844246858e Mon Sep 17 00:00:00 2001 From: Hayden Chow <kin3.chow@live.uwe.ac.uk> Date: Thu, 5 Dec 2024 00:54:00 +0000 Subject: [PATCH] edited --- src/task2.asm | 73 ++++++++++++++++++++++++++++----------------------- 1 file changed, 40 insertions(+), 33 deletions(-) diff --git a/src/task2.asm b/src/task2.asm index aba7735..e182067 100644 --- a/src/task2.asm +++ b/src/task2.asm @@ -1,64 +1,71 @@ +%include "asm_io.inc" -%include "asm_io.inc" ; segment .data - prompt_name db "Enter your name: ", 0 ; - prompt_number db "Enter a number between 50 and 100: ", 0 ; - welcome_msg db "Welcome, ", 0 ; - newline db 10, 0 ; + prompt_name db "What is your name? ", 0 ; + prompt_count db "Enter a number between 50 and 100: ", 0 ; + welcome_msg db "Welcome, ", 0 ; + newline db 10, 0 ; error_msg db "Error: The number must be between 50 and 100!", 0 ; segment .bss - user_name resb 50 ; - user_input resd 1 ; + user_name resb 50 ; + name_length resd 1 ; + print_count resd 1 ; -section .text +segment .text global asm_main asm_main: pusha ; - ; Ask the user for their name mov eax, prompt_name ; call print_string ; - call read_string ; - ; Ask the user for a number between 50 and 100 - mov eax, prompt_number ; + mov edi, user_name ; + xor ecx, ecx ; + +read_name_loop: + call read_char ; + cmp al, 10 ; + je name_input_done ; + mov [edi], al ; + inc edi ; + inc ecx ; + cmp ecx, 50 ; + jl read_name_loop ; + + mov [name_length], ecx ; + mov byte [edi], 0 ; + + mov eax, prompt_count ; call print_string ; - call read_int ; - mov [user_input], eax ; + call read_int ; + mov [print_count], eax ; - ; Check if the number is between 50 and 100 - mov eax, [user_input] ; + ; Validate the input range + mov eax, [print_count] ; cmp eax, 50 ; - jle invalid_input ; - cmp eax, 100 ; - jge invalid_input ; + jl invalid_input ; + cmp eax, 100 ; + jg invalid_input ; - ; If the number is valid, print the welcome message with the user's name + ; If input is valid, print the welcome message `print_count` times + mov ecx, [print_count] ; +print_loop: mov eax, welcome_msg ; call print_string ; mov eax, user_name ; call print_string ; - call print_nl ; - - ; Print the welcome message the number of times entered by the user - mov ecx, [user_input] ; -print_welcome: - mov eax, welcome_msg ; - call print_string ; call print_nl ; - loop print_welcome ; - - jmp end_program ; + loop print_loop ; + jmp exit_program ; invalid_input: - ; Print the error message if the input is invalid + ; Print the error message mov eax, error_msg ; call print_string ; call print_nl ; jmp asm_main ; -end_program: popa ; mov eax, 0 ; - ret + ret ; -- GitLab