diff --git a/asm_io.inc b/asm_io.inc deleted file mode 100644 index 9163be3955bb418e8b5d74d9004b38cc45f7cfa4..0000000000000000000000000000000000000000 --- a/asm_io.inc +++ /dev/null @@ -1,30 +0,0 @@ - extern read_int, print_int, print_string - extern read_char, print_char, print_nl - extern sub_dump_regs, sub_dump_mem, sub_dump_math, sub_dump_stack - -%macro dump_regs 1 - push dword %1 - call sub_dump_regs -%endmacro - - -; -; usage: dump_mem label, start-address, # paragraphs -%macro dump_mem 3 - push dword %1 - push dword %2 - push dword %3 - call sub_dump_mem -%endmacro - -%macro dump_math 1 - push dword %1 - call sub_dump_math -%endmacro - -%macro dump_stack 3 - push dword %3 - push dword %2 - push dword %1 - call sub_dump_stack -%endmacro \ No newline at end of file diff --git a/src b/src deleted file mode 100644 index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..0000000000000000000000000000000000000000 diff --git a/asm_io.asm b/src/asm_io.asm similarity index 100% rename from asm_io.asm rename to src/asm_io.asm diff --git a/src/driver.c b/src/driver.c new file mode 100644 index 0000000000000000000000000000000000000000..1c33ae595b144ddd1cd11b95bf6a6fc53989d4fc --- /dev/null +++ b/src/driver.c @@ -0,0 +1,6 @@ +int __attribute__((cdecl)) asm_main( void ); +int main() { +int ret_status; +ret_status = asm_main(); +return ret_status; +} \ No newline at end of file diff --git a/src/driver.o b/src/driver.o new file mode 100644 index 0000000000000000000000000000000000000000..d9c964d5c502ff05525b278dcc90cc8150982da0 Binary files /dev/null and b/src/driver.o differ diff --git a/worksheet1.asm b/src/task1.asm similarity index 72% rename from worksheet1.asm rename to src/task1.asm index 1ff3aeb354754b8f75a2a051a30f6bf78f50fd86..c561c1df8bd72193b24043b80771c6d06d3468ae 100644 --- a/worksheet1.asm +++ b/src/task1.asm @@ -13,10 +13,4 @@ add eax, [integer2] ; eax = int1 + int2 mov [result], eax ; result = int1 + int2 call print_int ; print result popa -mov eax, 0 - -% nasm -f elf one.asm -o one.o - -% gcc -m32 -c driver.c -o driver.o - -% gcc -m32 driver.o one.o asm_io.o -o a.out \ No newline at end of file +mov eax, 0 \ No newline at end of file diff --git a/src/task2.2.asm b/src/task2.2.asm new file mode 100644 index 0000000000000000000000000000000000000000..adbba83e0c8344be46a38b7c7ce98225fe4bf8f8 --- /dev/null +++ b/src/task2.2.asm @@ -0,0 +1,93 @@ + +%include "asm_io.inc" +segment .data + prompt_start db "Enter the start of the range: ", 0 ; + prompt_end db "Enter the end of the range: ", 0 ; + msg_result db "The sum of the array is: ", 0 ; + newline db 10, 0 ; Newline character + +segment .bss + array resd 100 ; + sum resd 1 ; + start resd 1 ; + end resd 1 ; + +segment .text +global asm_main + +asm_main: + pusha ; + mov ebx, array ; +init_array: + mov [ebx], ecx ; + add ebx, 4 ; + inc ecx ; + cmp ecx, 101 ; + jl init_array ; + + ; Sum the entire array + mov ecx, 100 ; + mov ebx, array ; + xor edx, edx ; +sum_array: + add edx, [ebx] ; + add ebx, 4 ; + loop sum_array ; + + ; Store the sum in the 'sum' variable + mov [sum], edx ; + + ; Print the result message + mov eax, msg_result ; + call print_string ; + mov eax, [sum] ; + call print_int ; + call print_nl ; + + ; Ask the user for the start of the range + mov eax, prompt_start ; + call print_string ; + call read_int ; + mov [start], eax ; + + ; Ask the user for the end of the range + mov eax, prompt_end ; + call print_string ; + call read_int ; + mov [end], eax ; + + ; Check if the range is valid + mov eax, [start] ; + mov ebx, [end] ; + cmp eax, ebx ; + jge invalid_range ; + + ; Sum the range between start and end + xor edx, edx ; + mov ecx, [start] ; + mov ebx, [end] ; +sum_range: + add edx, ecx ; + inc ecx ; + cmp ecx, ebx ; + jle sum_range ; + + ; Print the sum of the range + mov eax, "The sum of the range is: " ; Print result message + call print_string ; + mov eax, edx ; + call print_int ; + call print_nl ; + + jmp end_program ; + +invalid_range: + ; Print an error message if the range is invalid + mov eax, "Error: Invalid range. The start must be less than the end." ; + call print_string ; + call print_nl ; + +end_program: + popa ; + mov eax, 0 ; + ret diff --git a/src/task2.asm b/src/task2.asm new file mode 100644 index 0000000000000000000000000000000000000000..aba7735cef13fe45a224b33c5b98fd9c42e78db5 --- /dev/null +++ b/src/task2.asm @@ -0,0 +1,64 @@ + +%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 ; + error_msg db "Error: The number must be between 50 and 100!", 0 ; + +segment .bss + user_name resb 50 ; + user_input resd 1 ; + +section .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 ; + call print_string ; + call read_int ; + mov [user_input], eax ; + + ; Check if the number is between 50 and 100 + mov eax, [user_input] ; + cmp eax, 50 ; + jle invalid_input ; + cmp eax, 100 ; + jge invalid_input ; + + ; If the number is valid, print the welcome message with the user's name + 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 ; + +invalid_input: + ; Print the error message if the input is invalid + mov eax, error_msg ; + call print_string ; + call print_nl ; + jmp asm_main ; + +end_program: + popa ; + mov eax, 0 ; + ret