I am working through the Western Digital tutorial for RISC-V assembly. I am relatively new to assembly programming. So I have a few clarifying questions, and I can't seem to find a real dummy friendly tutorial.
Here is the code in question on GITHub.
# SPDX-License-Identifier: Unlicense
# Copyright (c) 2018 Western Digital Corporation or its affiliates.
.section .text
.align 2
.globl setupGPIO
.include "memory_map.inc"
.include "gpio.inc"
setupGPIO:
addi sp, sp, -16 # Allocate Stack Frame
#First Question
sw ra, 12(sp) # Save return address onto the stack
li t0, GPIO_CTRL_ADDR # Load the base GPIO address
li t1, GPIO_RGB_PINS # Get the RGP Pins offset
sw t1, GPIO_OUTPUT_EN(t0) # Enable RGB pins as output pins
sw t1, GPIO_OUTPUT_XOR(t0) # Set the XOR to that the pins are Active High
#Second Question
sw x0, GPIO_OUTPUT_VAL(t0) # Set all writable GPIO pins to zero
#Third Question
lw ra, 12(sp) # Restore the return address
addi sp, sp, 16 # Deallocate stack frame
ret
So, my questions are:
What is the 12 doing? I thought the stack pointer has already been shifted 16 bits in the previous instruction. I'm generally confused about why move down 16 bits in memory, but then add 12 back.
Also, is this syntax saying the same thing in x86 as[eax+12]?The destination operand has always been on the left, is this backwards only for the
x0register?I've noticed that we have not used the stack pointer at all, do we really need to do all this? Also, what is the difference between
swandlw? They have both been used and I'm not sure what they accomplish or why one would be used over the other.