From 32b37bb37401b7724e54d2661a2cd174281f30e2 Mon Sep 17 00:00:00 2001
From: Wesley Moore <wes@wezm.net>
Date: Sun, 9 Feb 2025 15:45:37 +1000
Subject: [PATCH] Print register names

---
 calc.s | 37 +++++++++++++++++++++++++++++--------
 1 file changed, 29 insertions(+), 8 deletions(-)

diff --git a/calc.s b/calc.s
index 2b783d9..58d09cf 100644
--- a/calc.s
+++ b/calc.s
@@ -11,9 +11,12 @@
 .section .rodata
 str: .ascii "Hello World!\n"
      .set str_size, .-str
+
 str2: .ascii "regdump\n"
       .set str2_size, .-str2
 
+regnames:
+  .ascii "x0", "ra", "sp", "gp", "tp", "t0", "t1", "t2", "s0", "s1", "a0", "a1", "a2", "a3", "a4", "a5"
 
 .section .bss
 buf: .skip 20        # room for 20 byte string
@@ -92,19 +95,37 @@ regdump:
     li a2, str2_size    # length of other string
     ecall               # invoke syscall to print the string
 
-    # as a test load something into buf and print it
-    la a1, buf          # load address of buf into a1
-    li a0, 'h
-    sb a0, 0(a1)
-    li a0, '\n
-    sb a0, 1(a1)
-    li a2, 2            # length of buf
+    li t1, 0
+    li t2, 16
 
+regdump_loop:
+    beq t1, t2, regdump_done
+    # a1 = regnames + (2 * t1)
+    # a1 = t1 << 1 + regnames
+    la a1, regnames     # load address of regnames
+    slli a2, t1, 1
+    add a1, a1, a2
+
+    # print the register name
     li t0, SYSWRITE     # "write" syscall
     li a0, 1            # 1 = standard output (stdout)
-    # li a2, buflen       # length of hello string
+    # a1 is the address of the string, calculated above
+    li a2, 2            # length of register name string
     ecall               # invoke syscall to print the string
 
+    # add newline
+    la a1, buf          # load address of buf into a1
+    li a0, '\n
+    sb a0, 0(a1)
+    li a2, 1            # length of buf
+    li t0, SYSWRITE     # "write" syscall
+    li a0, 1            # 1 = standard output (stdout)
+    ecall               # invoke syscall to print the string
+
+    addi t1, t1, 1
+    j regdump_loop
+
+regdump_done:
     addi sp, sp, 16   # deallocate stack space
     ret