From def3f62f7f8d6b5bd4b15500c7d11935540e81da Mon Sep 17 00:00:00 2001 From: Flavian Kaufmann Date: Wed, 15 May 2024 09:27:51 +0200 Subject: fixed relative memory addressing bug --- prog/link.ld | 4 +-- prog/src/main.c | 2 ++ prog/src/prog.s | 78 +++++++++++++++++++++++++++++++++++++++++ prog/src/prog.s.bak | 99 ----------------------------------------------------- 4 files changed, 82 insertions(+), 101 deletions(-) create mode 100644 prog/src/prog.s delete mode 100644 prog/src/prog.s.bak (limited to 'prog') diff --git a/prog/link.ld b/prog/link.ld index 4cc02ec..7bd11eb 100644 --- a/prog/link.ld +++ b/prog/link.ld @@ -43,7 +43,7 @@ SECTIONS .stack (NOLOAD) : { _stack_start = .; - . += 0x10; /* Adjust the size as needed */ + . += 0x80; /* Adjust the size as needed */ _stack_end = .; } > RAM @@ -51,7 +51,7 @@ SECTIONS .heap (NOLOAD) : { _heap_start = .; - . += 0x10; /* Adjust the size as needed */ + . += 0x80; /* Adjust the size as needed */ _heap_end = .; } > RAM } diff --git a/prog/src/main.c b/prog/src/main.c index 1ce067c..8bcdfe2 100644 --- a/prog/src/main.c +++ b/prog/src/main.c @@ -1,6 +1,8 @@ +extern void test_prog(void); int main(void) { + test_prog(); while (1) { } } diff --git a/prog/src/prog.s b/prog/src/prog.s new file mode 100644 index 0000000..ec227a9 --- /dev/null +++ b/prog/src/prog.s @@ -0,0 +1,78 @@ +.section .text +.globl test_prog + + +/* + addi t0, zero, 31 +reset_loop: + addi t6, zero, 0 +loop: + addi t6, t6, 1 + beq t6, t0, reset_loop + j loop +*/ + +test_prog: + li t0, 0xFFFFFFFF + li t1, 0x33333333 + li t2, 0x88888888 + li t3, 0x11111111 + + add t4, t1, t2 + sub t4, t1, t2 + slt t4, t3, t0 + slt t4, t0, t3 + sltu t4, t3, t0 + sltu t4, t0, t3 + and t4, zero, zero + and t4, zero, t0 + and t4, t0, t0 + or t4, zero, zero + or t4, zero, t0 + or t4, t0, t0 + xor t4, zero, zero + xor t4, zero, t0 + xor t4, t0, t0 + + beq t0, t0, branch_eq + j branch_eq_nt +end: + ret + +branch_eq_ret: + beq t0, t3, branch_ne + j branch_ne_nt + +branch_ne_ret: + addi sp, sp, -16 # Adjust stack pointer to make space for ra and s0 + sw ra, 0(sp) # Save ra to the stack + call func + lw ra, 0(sp) # Load ra from the stack + addi sp, sp, 16 # Restore stack pointer + + li t0, 0x00100000 + sw t1, 0(t0) + lw t2, 0(t0) + + j end + + +branch_eq: + addi t5, zero, 1 + j branch_eq_ret + +branch_eq_nt: + addi t5, zero, 2 + j branch_eq_ret + +branch_ne: + addi t5, zero, 3 + j branch_ne_ret + +branch_ne_nt: + addi t5, zero, 4 + j branch_ne_ret + +func: + addi t5, zero, 5 + ret diff --git a/prog/src/prog.s.bak b/prog/src/prog.s.bak deleted file mode 100644 index 63026c9..0000000 --- a/prog/src/prog.s.bak +++ /dev/null @@ -1,99 +0,0 @@ -.section .text -.globl _start - -_start: - - - j test_prog - - - -/* - addi t0, zero, 31 -reset_loop: - addi t6, zero, 0 -loop: - addi t6, t6, 1 - beq t6, t0, reset_loop - j loop -*/ - -halt_loop: - j halt_loop - -test_prog: - li t0, 0xFFFFFFFF - li t1, 0x33333333 - li t2, 0x88888888 - li t3, 0x11111111 - - add t4, t1, t2 - sub t4, t1, t2 - slt t4, t3, t0 - slt t4, t0, t3 - sltu t4, t3, t0 - sltu t4, t0, t3 - and t4, zero, zero - and t4, zero, t0 - and t4, t0, t0 - or t4, zero, zero - or t4, zero, t0 - or t4, t0, t0 - xor t4, zero, zero - xor t4, zero, t0 - xor t4, t0, t0 - - beq t0, t0, branch_eq - j branch_eq_nt - -branch_eq_ret: - beq t0, t3, branch_ne - j branch_ne_nt - -branch_ne_ret: - call func - - li t0, 0x00100000 - sw t1, 0(t0) - lw t2, 0(t0) - - j halt_loop - - -branch_eq: - addi t5, zero, 1 - j branch_eq_ret - -branch_eq_nt: - addi t5, zero, 2 - j branch_eq_ret - -branch_ne: - addi t5, zero, 3 - j branch_ne_ret - -branch_ne_nt: - addi t5, zero, 4 - j branch_ne_ret - -func: - addi t5, zero, 5 - ret - - - - - - - - - - -.section .data - - -.section .bss - -.section .stack - .space 0x1000 # Allocate stack space -stack_top: -- cgit v1.2.3