# ===================================================================
# 32-bit architecture
# ===================================================================
# print a float
#
# In RARS (RISC-V Assembler and Runtime Simulator), floating-point
# registers (f0 through f31) are 64 bits wide (double precision
# support / D-extension). RARS does not have single-precision float
# registers. Therefore, single-precision floats must be converted
# to double-precision floats to do things.
# ===================================================================
.data
NEWLINE: .asciz "\n"
x: .float 2.31e1
y: .float 23.1
z: .float 12.3
.text
.global main
# -------------------------------------------------------------------
# function: print float
#
# register a0 contains the address of the float to be printed
# -------------------------------------------------------------------
print_float:
# ---------------------------------------------------------------
# function Prologue
# ---------------------------------------------------------------
addi sp, sp, -4 # allocate space for a stack frame
sw ra, 0(sp) # Save return address
# ---------------------------------------------------------------
# function body
# ---------------------------------------------------------------
# print float
flw ft1, 0(a0) # load float into ft1
li a7, 2 # syscall number, print float
fmv.s fa0, ft1 # move ft1 to ft0
ecall # syscall
# print newline
li a7, 4 # syscall number, print string
la a0, NEWLINE # address of newline char/string
ecall # syscall
# ---------------------------------------------------------------
# function epilogue
# ---------------------------------------------------------------
lw ra, 0(sp) # restore return address
addi sp, sp, 4 # de-allocate stack frame space
ret # return to caller
# -------------------------------------------------------------------
# main
# -------------------------------------------------------------------
main:
# print floats
la a0, x
call print_float
la a0, y
call print_float
la a0, z
call print_float
# exit program
li a7, 93 # syscall number, exit program
li a0, 0 # return code
ecall # syscall
Here Is A Code Example From The Web
To convert a single-precision floating-point number to a
double-precision number in the RISC-V architecture using
the RARS (RISC-V Assembler and Runtime Simulator) simulator,
you use the fcvt.d.s instruction.
Here is a RISC-V function RISC-V that demonstrates how to
load a single-precision float, convert it to double-precision,
and check the results in RARS.
.data
pi_single: .float 3.141592 # A 32-bit single-precision float
.text
.globl main
main:
# 1. Load the single-precision float from memory into register fa0
flw fa1, pi_single, t0
# 2. Convert single-precision (fa1) to double-precision (fa0)
fcvt.d.s fa0, fa1
#3. print double (already in fa0)
li a7, 3
ecall
# 4. Exit program
li a7, 10
ecall
Many assemblers and simulators (like RARS) allow a
pseudo-form like flw ft0, label, t0 where the assembler
uses a temporary integer register (t0) behind the scenes
to compute the absolute address from the label.
Another Code Example From The Web
To print a double-precision floating-point number in RARS (RISC-V
Assembler and Runtime System), use service code 3 in register a7
with the double value loaded in register fa0.
.data
pi_double: .double 3.141592653589793 # A 64-bit double-precision float
.text
.globl main
main:
# 1. Load the double-precision value from memory into fa0
la t0, pi_double
fld fa0, 0(t0)
# 2. Set the syscall code for printing a double (3)
li a7, 3
ecall
# 3. Exit program
li a7, 10
ecall