From b48a1e88380f1c9b12a1ad11789fd8189e39ad59 Mon Sep 17 00:00:00 2001 From: Hayden Chow <kin3.chow@live.uwe.ac.uk> Date: Tue, 3 Dec 2024 20:21:10 +0000 Subject: [PATCH] edited --- asm_io.inc | 30 ---------- src | 0 asm_io.asm => src/asm_io.asm | 0 src/driver.c | 6 ++ src/driver.o | Bin 0 -> 1348 bytes worksheet1.asm => src/task1.asm | 8 +-- src/task2.2.asm | 93 +++++++++++++++++++++++++++++ src/task2.asm | 64 ++++++++++++++++++++ 8 files changed, 164 insertions(+), 37 deletions(-) delete mode 100644 asm_io.inc delete mode 100644 src rename asm_io.asm => src/asm_io.asm (100%) create mode 100644 src/driver.c create mode 100644 src/driver.o rename worksheet1.asm => src/task1.asm (72%) create mode 100644 src/task2.2.asm create mode 100644 src/task2.asm diff --git a/asm_io.inc b/asm_io.inc deleted file mode 100644 index 9163be3..0000000 --- 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 e69de29..0000000 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 0000000..1c33ae5 --- /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 GIT binary patch literal 1348 zcmb<-^>JflWMqH=Mh0dE1doB?4HHDh1Wai#@H6m%<=Ge*7(VmM{SNJX8r=NkL-QMf z7k~c$|IZ52+j$tw?sQS%=?3B0KPL}&v#1<qaCdgLQqTxZD$Oe?Rj|}E(KFCBECurn z>x>Na3{3QlG$G;)EDQ_`JPZsBtPBhc?tY;RObiSMKw``c3=AS*w=gg;Rs}II)^IRN z^RTdVFfuSGFfcI4K;?8mv@0J+E0Ys1t9vtZ?9szKEG!HR0t^fc4j?%O2CyAaCJF`e z15AL0;p2Z88$>gMTmoW1F*BMe%wQKoB^ekPm>EDJh0NzbaKQ4MXnZa-K06wpk--Wo z0`j8}0}}(X4k0Esa5zAffc<L*<1j${&yZ4-S(aL)m&_0!UtwXUm!4V@UyvMMl2MwM zt(REA5by5e@8swc@9*Xo>>3gu;^^e#8qbheoEx8;n3>1Gz|X+Iz{CTI2QFR)25C@O z@<GC%iHU*11tbrOIKAS^+>*p32EF2vA_$!UV-=<5=#`{alrZR}B$gyH=p_{wGk^v3 zu)9ewIX^cyHLrw0FE76&RnOfoRJXV!F*zH`PtPmWD=5k@NG&R<WY7Z{o|+M#R+N~V z%Al8ClwVqa6a)mBZ43+yf=H@BX&)48p!5jBpmYer@(c{{yZ};e!@$4*iW3kUgkk9j z6h5F>hNU|bCI$uxX!-_aFOXRv^Hrhdfzmige+Gy^!Z7o+85kHq>ItbcfSSh!vV?(w z0c0M?E|`%p^L(J{K<YtqIavGwQX2|Y2T}vVGe86q2B`sYQyCZ-K&cjLEdxj$EMJ1; zVQ#2_ng>(2g@J(~0HgqlVd^Rv7#NVl&xZ+;jzMt&O8X#npzr|M4>Jp71Pdbr!v_Wi x23`gR25wLqg{FUyS+KAJsr$mnz+ewzKrzT15EGOJkk!R7F);LkB%m0k4gjTwhcW;F literal 0 HcmV?d00001 diff --git a/worksheet1.asm b/src/task1.asm similarity index 72% rename from worksheet1.asm rename to src/task1.asm index 1ff3aeb..c561c1d 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 0000000..adbba83 --- /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 0000000..aba7735 --- /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 -- GitLab