From c97cb4c216a18d0d08893cedbb442126631bc95e Mon Sep 17 00:00:00 2001
From: Daniel Kibblewhite <daniel2.kibblewhite@live.uwe.ac.uk>
Date: Thu, 7 Nov 2024 10:49:27 +0000
Subject: [PATCH] started worksheet 2 (not finished ws1)

---
 Worksheet2/source/kernel.elf | Bin 0 -> 4544 bytes
 Worksheet2/source/link.ld    |  22 +++++++++
 Worksheet2/source/loader.asm |  17 +++++++
 Worksheet2/source/loader.o   | Bin 0 -> 560 bytes
 ws1/Task1.asm                |  21 ---------
 ws1/makefile                 |  13 ++++++
 ws1/src/task2.asm            |  86 +++++++++++++++++++++++++++++++++++
 ws1/task1.o                  | Bin 1008 -> 0 bytes
 8 files changed, 138 insertions(+), 21 deletions(-)
 create mode 100755 Worksheet2/source/kernel.elf
 create mode 100644 Worksheet2/source/loader.o
 delete mode 100644 ws1/Task1.asm
 create mode 100644 ws1/makefile
 create mode 100644 ws1/src/task2.asm
 delete mode 100644 ws1/task1.o

diff --git a/Worksheet2/source/kernel.elf b/Worksheet2/source/kernel.elf
new file mode 100755
index 0000000000000000000000000000000000000000..18e16939683d0011afd371865e03a36acbf5329b
GIT binary patch
literal 4544
zcmb<-^>JflWMqH=CI)5(5RZpJfWd@;f#HV$M8<?cfq{`hgMpQS1uV}176%am48jZy
zV9W|q3K9a*qj)p~MnhmU1V%$(Gz3ONU^E0qLtr!nMnhmU1V%$(Gz3ONV9<mB(}uOu
zAjkak4|=j=->!eBUjGAev0zBY9yB@t8XtfuU}5<9AJqScnZv;F@xKC05=?*mug1W@
z05TuUW%&4CpMilv5Hf(kz`(%BV9mh502*%q$qRr+7mD*si;`3IbMg~YQj7Ewi*p%#
z9o;>h<NZQ?om_($+<Y9}gBhGXT%EmxLwy;b%5w7a3!rQU2EF3S+>*p32EF2vA_$!U
zW0j;<lz?m^4ub**<X<+hasdXAvqAm^a~Z%~FcYMP3Cw~JA_5Ey4Gatn%nS?+><kPH
pEKs$aP%(%q28JmD3=B>%4mdO*96>a7*8~_C6d_Vz5@a8U0RUisJ$3*9

literal 0
HcmV?d00001

diff --git a/Worksheet2/source/link.ld b/Worksheet2/source/link.ld
index e69de29..a96d1b8 100644
--- a/Worksheet2/source/link.ld
+++ b/Worksheet2/source/link.ld
@@ -0,0 +1,22 @@
+ENTRY(loader)                /* The entry point for the kernel */
+
+SECTIONS {
+    . = 0x00100000;          /* Load the kernel code at 1 MB */
+    
+    .text : {                 /* Text (code) section */
+        *(.text)             /* Include all text sections */
+    }
+    
+    .rodata : {              /* Read-only data section */
+        *(.rodata*)          /* Include all read-only data sections */
+    }
+
+    .data : {                /* Data section */
+        *(.data)             /* Include all data sections */
+    }
+
+    .bss : {                 /* BSS section */
+        *(.bss)              /* Include all BSS sections */
+        *(COMMON)            /* Include common sections */
+    }
+}
diff --git a/Worksheet2/source/loader.asm b/Worksheet2/source/loader.asm
index e69de29..8a95ce3 100644
--- a/Worksheet2/source/loader.asm
+++ b/Worksheet2/source/loader.asm
@@ -0,0 +1,17 @@
+global loader              ; Entry point for the kernel
+
+section .text              ; Start of the code section
+MAGIC_NUMBER equ 0x1BADB002     ; Define the magic number constant
+FLAGS equ 0x0                   ; Multiboot flags
+CHECKSUM equ -MAGIC_NUMBER      ; Calculate the checksum
+
+; Align code at 4 bytes boundary
+    align 4
+    dd MAGIC_NUMBER             ; Magic number
+    dd FLAGS                    ; Flags
+    dd CHECKSUM                 ; Checksum
+
+loader:                        ; Entry point label
+    mov eax, 0xCAFEBABE         ; Write 0xCAFEBABE to the eax register
+.loop:
+    jmp .loop                   ; Loop forever
diff --git a/Worksheet2/source/loader.o b/Worksheet2/source/loader.o
new file mode 100644
index 0000000000000000000000000000000000000000..dfcbed2f3a4c859a2a16476a9016f153df56d614
GIT binary patch
literal 560
zcmb<-^>JflWMqH=Mh0dE1doBi0V-hvrZpH?8JNH<QVEcmAj}5UD8R_TAPi-LC;<?`
zz`(%Hz`(!^WrHXKMg|5&5Ce)qW`LN23=AkPNnm7PXkcJqV1b$eqCsX#qM7%Ak%7Sp
zWB?Sy%!6_nm^Q4H2C@J72R+%bZ`Z$5um6F#2&`9<T2aEFSDaB?QdE+d#GqGPnG2yI
ze1uA@EM)sw7(V_-Rs;%pP?*5nz`*eFzXGxXhL8W%7#J8pZi5Ja{BOp<zyJyh2%nKb
zpMima2f|?x0L5f+erZv1s(wy>VoGX}USe@BgRi5zr*phtsIQZ25QCeKqkAxevxlp*
VcW|gL14J2=(#y%uFMyf_qX7T9J4^ro

literal 0
HcmV?d00001

diff --git a/ws1/Task1.asm b/ws1/Task1.asm
deleted file mode 100644
index 8177079..0000000
--- a/ws1/Task1.asm
+++ /dev/null
@@ -1,21 +0,0 @@
-%include "src/asm_io.inc"
-
-segment .data
-    integer1 dd 15      ; first int
-    integer2 dd 6       ; second int
-
-segment .bss
-    result resd 1       ; space for the result
-
-segment .text
-global asm_main
-
-asm_main:
-    pusha                     ; save all registers
-    mov eax, [integer1]      ; load integer1 into eax
-    add eax, [integer2]      ; add integer2 to eax
-    mov [result], eax        ; store the result
-    call print_int           ; print the result
-    popa                     ; restore all registers
-    mov eax, 0               ; return 0
-    ret
diff --git a/ws1/makefile b/ws1/makefile
new file mode 100644
index 0000000..96a76c4
--- /dev/null
+++ b/ws1/makefile
@@ -0,0 +1,13 @@
+all: task1
+
+task1: task1.o asm_io.o driver.o
+    gcc -m32 -o task1 task1.o asm_io.o driver.o
+
+task1.o: task1.asm
+    nasm -f elf task1.asm -o task1.o
+
+asm_io.o: asm_io.asm
+    nasm -f elf asm_io.asm -o asm_io.o
+
+driver.o: driver.c
+    gcc -m32 -c driver.c -o driver.o
diff --git a/ws1/src/task2.asm b/ws1/src/task2.asm
new file mode 100644
index 0000000..612195c
--- /dev/null
+++ b/ws1/src/task2.asm
@@ -0,0 +1,86 @@
+%include "asm_io.inc"
+
+section .bss
+    name resb 50                  ; Reserve space for the name (max 50 characters)
+    count resb 4                  ; Space to store the count (4 bytes)
+
+section .text
+    global _start
+
+_start:
+    ; Prompt for name
+    mov eax, 4                    ; syscall: sys_write
+    mov ebx, 1                    ; file descriptor: stdout
+    mov ecx, promptName           ; message: "Enter your name: "
+    mov edx, promptNameLen        ; message length
+    int 0x80                      ; call kernel
+
+    ; Read name from user
+    mov eax, 3                    ; syscall: sys_read
+    mov ebx, 0                    ; file descriptor: stdin
+    mov ecx, name                 ; buffer
+    mov edx, 50                   ; number of bytes to read
+    int 0x80                      ; call kernel
+
+    ; Prompt for number of times to print welcome message
+    mov eax, 4                    ; syscall: sys_write
+    mov ebx, 1                    ; file descriptor: stdout
+    mov ecx, promptCount          ; message: "Enter number of times (51-99): "
+    mov edx, promptCountLen       ; message length
+    int 0x80                      ; call kernel
+
+    ; Read number of times from user (buffer for 4 bytes)
+    mov eax, 3                    ; syscall: sys_read
+    mov ebx, 0                    ; file descriptor: stdin
+    mov ecx, count                ; buffer
+    mov edx, 4                    ; number of bytes to read
+    int 0x80                      ; call kernel
+
+    ; Convert input string to integer (assumes input is in ASCII)
+    mov eax, [count]              ; get input value (in ASCII)
+    sub eax, '0'                  ; convert ASCII to integer (this setup assumes single digit input)
+
+    ; Validate the input
+    cmp eax, 51
+    jl print_error                ; If less than 51, jump to error
+    cmp eax, 99
+    jg print_error                ; If greater than 99, jump to error
+
+    ; Repeat welcome message
+    mov ebx, eax                  ; Store the count in EBX for the loop
+print_welcome:
+    mov eax, 4                    ; syscall: sys_write
+    mov ecx, welcomeMsg           ; message: "Welcome to the program!"
+    mov edx, welcomeMsgLen        ; message length
+    int 0x80                      ; call kernel
+
+    dec ebx                        ; Decrement the counter
+    jnz print_welcome             ; Loop until counter is zero
+
+    ; Exit program
+    mov eax, 1                    ; syscall: sys_exit
+    xor ebx, ebx                  ; status: 0
+    int 0x80                      ; call kernel
+
+print_error:
+    ; Print error message
+    mov eax, 4                    ; syscall: sys_write
+    mov ebx, 1                    ; file descriptor: stdout
+    mov ecx, errorMsg             ; message: "Error: Number must be between 51 and 99"
+    mov edx, errorMsgLen          ; message length
+    int 0x80                      ; call kernel
+
+    ; Exit program
+    mov eax, 1                    ; syscall: sys_exit
+    xor ebx, ebx                  ; status: 0
+    int 0x80                      ; call kernel
+
+section .data
+    promptName db "Enter your name: ", 0
+    promptNameLen equ $ - promptName
+    promptCount db "Enter number of times (51-99): ", 0
+    promptCountLen equ $ - promptCount
+    welcomeMsg db "Welcome to the program!", 10
+    welcomeMsgLen equ $ - welcomeMsg
+    errorMsg db "Error: Number must be between 51 and 99", 10
+    errorMsgLen equ $ - errorMsg
diff --git a/ws1/task1.o b/ws1/task1.o
deleted file mode 100644
index f78efb05cb60ce9bef772eff36e56dcee269b2a7..0000000000000000000000000000000000000000
GIT binary patch
literal 0
HcmV?d00001

literal 1008
zcmb<-^>JflWMqH=Mh0dE1doBi0V-hvrZpHi7+An8QVEcmAj}Na*ucoZzyW20D4412
z3=nacx(SR7C^o{>@h~ue-N^>h%)r0^QYQ;#gD3$I!N9;E#K6D+GYcfNfsuj148(w9
zkefhE83qOhCJ+OPKQJ;d7%(z0fXoNE6J#&QOcgZq6qp$p7DM%bD42OV3=9mMAOR@8
zz|6p)z`(%33e^uX4`ePs1H=xfGKPeOAQm$#NFHqNi$DMW|4-Zj5<Lte^imQ_5*hT8
zii;WaN>VFI81#xWic5-05|bG8iYs#=G?ZVIngdY-(uf_y+zXOrVfgqTM8hy@N&tl;
zatML=%nXe%#bBC|p^E`Iz8D!MgT-M469YGl$-uw>3QrL<z8o4~8;x&@#)qX(kTA$R
zPY?r&LHuwi8$^NlDIf+EgZM>IHi!c8XMq?{EC5P5A&JG=hI)y`xeP_Ai7D}!c_j=5
zMVWaeNUY+LqRhN>h<tKJVi8mU5;HG{p|~_DKBY9bAigLyy%@pEP0dB(B$i|#c*P}&
z$=M8<c_pdosYQk`h7m(iYH?{!2}5FWZhUTHW**eBPzn@7j7*R`1fqFC`Ggsg512R^
F7ytpVX1M?W

-- 
GitLab