From e54e352a6e2a0b5589ae79de81b79afe43662210 Mon Sep 17 00:00:00 2001 From: Ed_ Date: Sat, 2 Aug 2025 13:08:30 -0400 Subject: [PATCH] attempts at warmup exercises. --- code/warmup/exercise1.s | 56 --------------------------------- code/warmup/exercise_1.s | 32 +++++++++++++++++++ code/warmup/exercise_2.s | 47 +++++++++++++++++++++++++++ code/warmup/exercise_3.s | 68 ++++++++++++++++++++++++++++++++++++++++ docs/readme.md | 3 ++ scripts/build.ps1 | 4 ++- 6 files changed, 153 insertions(+), 57 deletions(-) delete mode 100644 code/warmup/exercise1.s create mode 100644 code/warmup/exercise_1.s create mode 100644 code/warmup/exercise_2.s create mode 100644 code/warmup/exercise_3.s create mode 100644 docs/readme.md diff --git a/code/warmup/exercise1.s b/code/warmup/exercise1.s deleted file mode 100644 index 3446e28..0000000 --- a/code/warmup/exercise1.s +++ /dev/null @@ -1,56 +0,0 @@ -.psx -.create "exercise1.bin", 0x80010000 - -; Entry Point of Code -.org 0x80010000 - -; Constant declaration -BASE_ADDR equ 0x0000 - - -; Symbol Alias Table - -; Instructions -load_imm equ li - -; Registers -rtemp_0 equ $t0 -rtemp_1 equ $t1 -rtemp_2 equ $t2 - -.macro test,dst_reg,value - li dst_reg,value -.endmacro - -.macro test2,reg - li reg,0xA000 -.endmacro - -.macro myli,dest,value - .if value & ~0xFFFF - ori dest,r0,value - .elseif (value & 0xFFFF8000) == 0xFFFF8000 - addiu dest,r0,value & 0xFFFF - .elseif (value & 0xFFFF) == 0 - lui dest,value >> 16 - .else - lui dest,value >> 16 + (value & 0x8000 != 0) - addiu dest,dest,value & 0xFFFF - .endif -.endmacro - -.macro store_word, src_reg, dst_address - sw src_reg, dst_address -.endmacro - -Main: - myli $t0, 0xA000 - li rtemp_1, 0xA100 ; $t1 = 0xA100 - li rtemp_2, 0x11111111 ; $t2 = 0x11111111 - -Loop: - sw $t2, BASE_ADDR($t0) ; Store byte at 0x0000 + t0 - addi $t0, $t0, 4 ; ++ t0 - blt $t0, $t1, Loop ; while (t0 < t1) keep looping - -.close diff --git a/code/warmup/exercise_1.s b/code/warmup/exercise_1.s new file mode 100644 index 0000000..5882afd --- /dev/null +++ b/code/warmup/exercise_1.s @@ -0,0 +1,32 @@ +.psx +.create "exercise_1.bin", 0x80010000 + +; Entry Point of Code +.org 0x80010000 + +; Constant declaration +BASE_ADDR equ 0x0000 + +; Symbol Alias Table + +; Instructions +load_imm equ li ; dst_reg, immeidate value +store_word equ sw ; src_reg, dst_address + +; Registers +rtemp_0 equ $t0 +rtemp_1 equ $t1 +rtemp_2 equ $t2 + +main: + ; TODO: + ; 1. Load $t0 with the immediate decimal value of 1 + ; 2. Load $t1 with the immediate decimal value of 256 + ; 3. Load $t2 with the immediate decimal value of 17 + + ; Attempt: + load_imm rtemp_0, 1 + load_imm rtemp_1, 256 + load_imm rtemp_2, 17 +end: +.close diff --git a/code/warmup/exercise_2.s b/code/warmup/exercise_2.s new file mode 100644 index 0000000..36f0fcc --- /dev/null +++ b/code/warmup/exercise_2.s @@ -0,0 +1,47 @@ +.psx +.create "exercise_2.bin", 0x80010000 + +; Entry Point of Code +.org 0x80010000 + +; Constant declaration +BASE_ADDR equ 0x0000 + +; Symbol Alias Table + +; Instructions +; Load +load_imm equ li ; dst_reg, immeidate value +; Store +store_word equ sw ; src_reg, dst_address +; Addition +add_s equ add ; dst_reg, reg_a, reg_b (signed) +add_u equ add ; dst_reg, reg_a, reg_b (unsigned) +add_si equ addi ; dst_reg, src_reg, immediate value (signed) +add_ui equ addiu ; dst_reg, src_reg, immediate value (unsigned) +; Branch +branch_equal equ beq ; reg, value(reg, immediate), dst_label + +; Registers +; Temporaries, may be changed by subroutines +rtemp_0 equ $t0 +rtemp_1 equ $t1 +rtemp_2 equ $t2 + +main: + ; TODO: + ; 1. Start $t0 with the value 1 and $t1 with the value 0 + ; 2. Loop, incrementing $t0 until it reaches the value 10 + ; 3. Keep adding and accumulating all values of $t0 inside $t1 + + ; Attempt: + load_imm rtemp_0, 1 + load_imm rtemp_1, 0 +loop: + add_s rtemp_1, rtemp_1, rtemp_0 + add_si rtemp_0, rtemp_0, 1 + branch_equal rtemp_0, 10, end + nop + j loop +end: +.close diff --git a/code/warmup/exercise_3.s b/code/warmup/exercise_3.s new file mode 100644 index 0000000..4a9834a --- /dev/null +++ b/code/warmup/exercise_3.s @@ -0,0 +1,68 @@ +.psx +.create "exercise_3.bin", 0x80010000 + +; Entry Point of Code +.org 0x80010000 + +; Constant declaration +BASE_ADDR equ 0x0000 + +; Symbol Alias Table + +; Instructions +; Load +load_imm equ li ; dst_reg, immeidate value +; Store +store_word equ sw ; src_reg, dst_address +; Addition +add_s equ add ; dst_reg, reg_a, reg_b (signed) +add_u equ add ; dst_reg, reg_a, reg_b (unsigned) +add_si equ addi ; dst_reg, src_reg, immediate value (signed) +add_ui equ addiu ; dst_reg, src_reg, immediate value (unsigned) +; Subtraction +sub_s equ sub ; +sub_u equ subu ; +; Branch +branch_equal equ beq ; reg, value(reg, immediate), dst_label +branch_gt_equal equ bge ; reg, value(reg, immediate), dst_label +branch_lt equ blt ; reg, value(reg, immediate), dst_label + +; Registers +; Temporaries, may be changed by subroutines +rtemp_0 equ $t0 +rtemp_1 equ $t1 +rtemp_2 equ $t2 + +; /* C code: */ +; main() { +; int num; // Assume num is loaded in $t0 +; int den; // Assume den is loaded in $t1 +; int res; // Assume res is loaded in $t2 +; num = 27; // Or any other number that we want +; den = 3; // Or any other number that we want +; res = 0; +; while (num >= den) { +; num -= den; +; res++; +; } +; } + +main: +; TODO: +; 1. Initialize $t0 to 27 (or any other value) +; 2. Initialize $t1 to 3 (or any other value) +; 3. Initialize $t2 (res) with 0 +; 4. While ($t0 >= $t1) { +; 5. Subtract $t0-$t1 and store it back into $t0 +; 6. Increment $t2 +; 7. } + +; Attempt: + load_imm rtemp_0, 27 + load_imm rtemp_1, 3 + load_imm rtemp_2, 0 + loop: :: branch_lt rtemp_0, rtemp_1, loop_break :: nop ; loop if < rtemp_0, rtemp_1 { + sub_s rtemp_0, rtemp_0, rtemp_1 ; -= rtemp_0, rtemp_1 + add_si rtemp_2, rtemp_2, 1 ; ++ rtemp_2 + j loop :: loop_break: ; } +.close diff --git a/docs/readme.md b/docs/readme.md new file mode 100644 index 0000000..9bcec77 --- /dev/null +++ b/docs/readme.md @@ -0,0 +1,3 @@ +# Docs + +[psx-spx: Playstation Specifications](https://psx-spx.consoledev.net) diff --git a/scripts/build.ps1 b/scripts/build.ps1 index 64b92be..9cd45e0 100644 --- a/scripts/build.ps1 +++ b/scripts/build.ps1 @@ -33,4 +33,6 @@ function build-program { param( pop-location } # build-program 'fillmem' 'fillmem' -build-program 'warmup' 'exercise1' \ No newline at end of file +# build-program 'warmup' 'exercise_1' +# build-program 'warmup' 'exercise_2' +build-program 'warmup' 'exercise_3' \ No newline at end of file