Assembly Cheatsheet - RISC V

.global _start

.data
	#data

.text

_start:
	#code
	#...

Write to stdout
stdout -> a0, 1
message -> a1, msg
length -> a2, msglen
write call -> a7, 64

Exit
exit status -> a0, code
exit code -> a7, 93

**Reading string

.data
	buffer .space 256

.text
.global _start

_start:
	la a0, buffer ; load bufferspace
	li a1, 256 ; make space for the input
	call read_string ; run function

; function returned the string into 'buffer'

; function:
read_string:
	addi sp, sp, -16 ; make space on stack for 16 bits (int 256)
	sw ra 12(sp) ; make space for return address
	add s0, a0, x0 ; copy the input (buffer) into saved temp reg
	add s1, a1, x0 ; copy the size of the buffer

	li a7, 63 ; read call
	li a0, 0 ; STDIN
	add a1, s0, x0 ; copy buffer address into function call
	add a2, s1, x0 ; copy buffer size into function call
	ecall

	lw ra, 12(sp) ; load address of gotten word 
	lw s0, 8(sp) ; save inputted word
	addi sp, sp, 16 ; return stack pointer
	ret ; return the function

Read
STDIN -> a0, 0
read call -> a7, 63