What is machine code (in hex) for
addi x5, x0, 32
- I type
- find opcode of addi on sheet = 3
- 32 is 2^5 so it’s 100000
- rs1: x0 in binary is 00000
- rd: x5 in binary 00101
- the left most are all 0’s so 000000
0x 0 2 0 0 0 2 9 3
What is assembly for :
0x 0031 0023
- Convert to binary
0000 0000 0011 0001 0000 0000 0010 0011
- Look at sheet to determine upcode 0100011 type. It is S type. Stands for store now we have the s format.
- Divide the fields in correct format of S type.
- look at the sheet next to upcode to find what funct3 is. It is a sb.
sb x3, 0 (x2) 1. x2 is from rs1 2. x3 is from rs2 we don’t want to change the location of rs2 that is why the immediate is separated. all because of the hardware, by minizing hardware complexity and maximizing performance.
Examble
addi x1, x0, 32 ; x1 = 32 (0x20)
addi x2, x0, 64 ; x2 = 64 (0x40)
sw x1, 0 (x2) ;takes what's in x1 and stores into x2 which is address 40???
Example
Base address of array A[] is in x10. Assume RV64I. Variable h is in x21. A[30] = h + A[30] + 1.
- Array is stored in Memory
- A is of double words
- Base address points to A[0].
- If we increment we get to the element of the array A[1], up to A[31] in total.
- The one we want is store at the very end back to A[30].
Steps:
- Let’s use temporary register x22.
- x22 = A[30] (load from memory)
- ld x22 , 240 (x10) ; (x22 + A[30])
- x10 is base address, grabbing A[0], 240: 8 x 30 because every double word contains 8 bytes and we want to get from A[0] to A[30]
- addi x22, x22, 1 (+ 1)
- add x22, x21, x22 (+ h)
- sd x22, 240(x10)
- storying x22.