RISC-V assembly Language - Read Integer From Console

# ================================================================= # 32-bit architecture # ================================================================= # read an integer from input console # ================================================================= .data prompt: .asciz "Enter an integer: " .text .global main # ----------------------------------------------------------------- # function: print a prompt # ----------------------------------------------------------------- print_prompt: # ------------------------------------------------------------- # function prologue # ------------------------------------------------------------- addi sp, sp, -4 # allocate stack frame sw ra, 0(sp) # save caller's return address # ------------------------------------------------------------- # function body # ------------------------------------------------------------- li a7, 4 # syscall number, print_string la a0, prompt # address of prompt string ecall # syscall # ------------------------------------------------------------- # function epilogue (returned value is in a0) # ------------------------------------------------------------- lw ra, 0(sp) # restore caller's return address addi sp, sp, 4 # de-allocate stack frame ret # return to caller # ----------------------------------------------------------------- # function: read integer # ----------------------------------------------------------------- read_integer: # ------------------------------------------------------------- # function prologue # ------------------------------------------------------------- addi sp, sp, -4 # allocate stack frame sw ra, 0(sp) # save caller's return address # ------------------------------------------------------------- # function body # ------------------------------------------------------------- li a7, 5 # syscall number, read integer # from the input console ecall # syscall mv t2, a0 # save returned integer li a7, 1 # syscall number, print integer ecall # syscall # ------------------------------------------------------------- # function epilogue (returned is already in a0) # ------------------------------------------------------------- lw ra, 0(sp) # restore caller's return address addi sp, sp, 4 # de-allocate stack frame ret # return to caller # ----------------------------------------------------------------- # main # ----------------------------------------------------------------- main: call print_prompt # call print string call read_integer # call read integer # exit program li a7, 10 ecall