RISC-V

Example0:

Here is example code for Venus with printouts:
.data
welcome: .asciiz "Hello World!\n"
qtest: .asciiz "What is 5 + 20?\n"
a1text: .asciiz "The Answer is:\n"

.text
main:
li a0 4
la a1 welcome
ecall
li a0 4
la a1 qtest
ecall
li a0 4
la a1 a1text
ecall
li a4 5
li a5 20
li a0 1
add a1 a4 a5
ecall

Example1:

        .text
        .globl main
main:
        addi sp,sp,-32         # Stack frame is 32 bytes long
        sw ra,20(sp)          # Save return address
        sw fp,16(sp)          # Save old frame pointer
        addi fp,sp,28        # Set up frame pointer
        
        li a0,10               # Put argument (10) in $a0
        jal fact                # Call factorial function
        mv t0,s2            # Move fact result to $t0 (temporary)
        la a1,$LC              # Put format string in $a0
        li a0,4                 # System call code for print_str
        ecall                   # Make system call
        mv a1, t0           # Move $t0 factorial result to $a0 (argument)
        li a0, 1                # System call code for print_int
        ecall                   # Make system call
        
        lw ra,20(sp)          # Restore return address
        lw fp,16(sp)          # Restore frame pointer
        addi sp,sp,32        # Pop stack frame
        
        li a0,10
        ecall                   #exit Program

        .data
$LC:
        .asciiz "The factorial of 10 is "

    .text
fact:
        addi sp,sp,-32          # Stack framee is 32 bytes long
        sw ra,20(sp)            # Save return address
        sw fp,16(sp)            # Save frame pointer
        addi fp,sp,28       # Set up frame pointer
        sw a0,0(fp)         # Save argument (n)
        
        lw s2,0(fp)           # Load n
        bgtz s2,$L2            # Branch if n > 0
        li s2,1                # Return 1
        j $L1                  # Jump to code to return
$L2:
        lw s3,0(fp)           # Load n
        addi s2,s3,-1          # Compute n - 1
        mv a0,s2            # Move value to $a0
        jal fact                # Call factorial function
        lw s3,0(fp)           # Load n
        mul s2,s2,s3         # Compute fact(n-1) * n
        

$L1:                            # Result is in $v0
        lw ra, 20(sp)         # Restore $ra
        lw fp, 16(sp)         # Restore $fp
        addi sp, sp, 32      # Pop stack
        jr ra                  # Return to caller
        


Last Modified: 2022-05-31 12:23:24 -0500