Skip to content

Instantly share code, notes, and snippets.

@KiyonoKara
Last active December 6, 2023 20:06
Show Gist options
  • Save KiyonoKara/898dcfacf896afbfaf2af7cc9b459f6d to your computer and use it in GitHub Desktop.
Save KiyonoKara/898dcfacf896afbfaf2af7cc9b459f6d to your computer and use it in GitHub Desktop.
Loading & Using Floats in RISC-V Assembly Example
# RISC-V Assembly example program
# RISC-V Assembly program adds 7.0 to a value if it's less than 7.0, otherwise it divides the value by 7.0
# Created: 2023-12-06
.data
# value1 = 35.0
value1: .float 35.0
seven: .float 7.0
# result = 0.0
result: .float 0.0
newline: .asciz "\n"
.text
main:
# Load value1 into f1
la t1, value1
flw f1, 0(t1)
# Load 7.0 into f2
la t2, seven
flw f2, 0(t2)
# Check if value1 is less than 7.0
flt.s t3, f1, f2
# Else value1 >= 7
beqz t3, else
# If value1 < 7
bnez, t3, if
if:
# 7.0 + value1
fadd.s f3, f2, f1
jal end
else:
# value1 / 7.0
fdiv.s f3, f1, f2
jal end
end:
# result = f3
fmv.s fa0, f3
li a7, 2
ecall
la a0, newline
li a7, 4
ecall
li a7, 10
ecall
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment