RISC-V Assembler Program Demo

# ======================================================================== # 32-bit architecture print string in reverse using recursion # ======================================================================== # ------------------------------------------------------------------------ # data # ------------------------------------------------------------------------ .data newline: .asciz "\n" string: .asciz "Wow, does it work?" # ------------------------------------------------------------------------ # text # ------------------------------------------------------------------------ .text .global main print_reverse_string: # -------------------------------------------------------------------- # function Prologue # -------------------------------------------------------------------- addi sp, sp, -8 # allocate space for stack frame sw ra, 4(sp) # save return address sw a0, 0(sp) # save current char address # -------------------------------------------------------------------- # function body # -------------------------------------------------------------------- # end of string? current char == 0, return to caller lbu t0, 0(a0) # load current char (byte unsigned) beq t0, zero, exit_print_reverse_string # -------------------------------------------------------------------- # recursively call function # -------------------------------------------------------------------- lw a0, 0(sp) # address of current char addi a0, a0, 1 # increment address call print_reverse_string # -------------------------------------------------------------------- # print current character # -------------------------------------------------------------------- lw t0, 0(sp) # address of current char li a7, 11 # syscall code 11, print character lbu a0, 0(t0) # load current char (byte unsigned) ecall # syscall # -------------------------------------------------------------------- # function epilogue (return to caller) # -------------------------------------------------------------------- exit_print_reverse_string: lw ra, 4(sp) # restore return address lw a0, 0(sp) # restore a0 addi sp, sp, 8 # de-allocate stack frame ret # return to caller # ------------------------------------------------------------------------ # main # ------------------------------------------------------------------------ main: # call print_reverse_string la a0, string # address of current char call print_reverse_string # exit program, returned code is zero li a0, 0 # returned code li a7, 93 # syscall number, exit program ecall # syscall