diff --git a/src/task2_1.asm b/src/task2_1.asm new file mode 100644 index 0000000000000000000000000000000000000000..bd2b0567178b646932d6fd04a70992d4df5dbfde --- /dev/null +++ b/src/task2_1.asm @@ -0,0 +1,81 @@ +segment .data + prompt db "Enter your name: ", 0 + format db "%s", 0 + welcome_msg db "Welcome, %s!", 0 + newline db 10, 0 + count_prompt db "Enter the number of times to print the welcome message: ", 0 + count_format db "%d", 0 + invalid_count_msg db "The number of times to print the welcome message must be greater than 50 and less than 100!", 0 + +segment .bss + name resb 50 ; Reserve space for the name (50 bytes) + count resd 1 ; Reserve space for the count (4 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 count prompt + push count_prompt + call printf + add esp, 4 ; Clean up the stack + + ; Read the count + push count ; Address of the buffer to store the count + push count_format ; Format string for scanf + call scanf + add esp, 8 ; Clean up the stack (2 arguments) + + ; Check if the count is valid + mov eax, [count] + cmp eax, 50 + jl invalid_count + cmp eax, 100 + jg invalid_count + + ; Print the welcome message + mov ecx, [count] + mov ebx, ecx ; Sao chép giá trị của ecx vào ebx + print_welcome: + push name ; Push the name to print + push welcome_msg + call printf + add esp, 8 ; Clean up the stack (2 arguments) + dec ebx ; Giảm giá trị của ebx + jnz print_welcome ; Nếu ebx khác 0 thì tiếp tục loop + + ; 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 + +invalid_count: + ; Print the invalid count message + push invalid_count_msg + call printf + add esp, 4 ; Clean up the stack + + ; Exit the program + mov eax, 1 ; syscall: exit + xor ebx, ebx ; exit code 0 + ret + int 0x80 \ No newline at end of file