# RISC-V assembly program implementing a calculator.

.org 0
# Provide program starting address to linker
.global _start

.extern regdump

/* newlib system calls */
.set SYSEXIT,  93
.set SYSWRITE, 64

# .section .rodata

# .section .bss

.text
_start:
    # do some adding
    li a0, 0x80000000   # 0.5
    li a1, 1            # 1
    li a2, 0x80000000   # 0.5
    li a3, 1            # 1
    jal add64
    #add t1, a0, 0       # copy a0 to t0 as end with overwrite it
    jal regdump

    li t0, SYSEXIT      # "exit" syscall
    add a0, x0, 0       # Use 0 return code
    ecall               # invoke syscall to terminate the program

# 64-bit integer addition
#   arguments:
#       a0: x lower 32 bits
#       a1: x upper 32 bits
#       a2: y lower 32 bits
#       a3: y upper 32 bits
#   return:
#       a0: x+y lower 32 bits
#       a1: x+y upper 32 bits
#
add64:
    add  a0, a0, a2  # add lower 32 bits
    add  t0, a1, a3  # add upper 32 bits
    sltu t1, a0, a2  # if lower 32-bit sum < a2 then set t1=1 (carry bit)
    add  a1, t0, t1  # upper 32 bits of answer (upper sum + carry bit)
    ret