1/46
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced |
---|
No study sessions yet.
.ascii vs .asciz
.ascii: No null terminating
.asciz: Produces null terminating
Null terminating different from newline (\n)
How many bits can each register hold?
32 bits / 4 bytes = 1 word
Number of bits it takes to store an address
32 bits / 4 bytes = 1 word
How many registers does RISC-V have?
32 registers
rd meaning on reference card
desination register
rs1/rs2 meaning on reference card
first source register/second source register
imm meaning on reference card
immediate - a constant number
pseudocode for how you would do this in risc-v
a = b + c + d - e
temp = b + c
temp = temp + d
a = temp - e
Rars code to do “a = b”.
a is stored in s0
b is stored in s1
add s0, s1, zero
How to subtract immediates in rars
f = g - 10
f is stored in s0
g is stored in s1
addi s0, s1, -10
System call syntax in RISC-V
addi a7, zero, sys_call_number
ecall
a7 is special register for system calls
Call is stored in a0
Sys call numbers
Remember - int, float, double, string
1 - print int
2 - print float
3 - print double
4 - print string
5 - read int
6 - read float
7 - read double
8 - read string
10 - exit
Explain each memory segment
Stack: stores local variables and function memory, i.e. s0, s1
Dynamic Data: Used when a .space is used in .data segment
Static Data: Used when a .word is used in .data segment
Text: Stores the machine code version of the assembly code
Reserved: Off use for code and reserved for special uses.
Each memory address points to a specific ___ (byte or bit?)
byte, bits aren’t stored.
If 0×1000 is a byte, next byte is at 0×1001
Sequential address of words differ by __ (the number)
4, since 1 word is 4 bytes
Memory —> Register
lw, load word
Register —> Memory
sw, store word
Load word syntax
lw rd, byte_offset(rs1)
rs1 = register containing pointer to memory
rd now contains the value at rs1 + byte_offset
Store word syntax
sw rs2, byte_offset(rs1)
rs2 = register containing memory you want to store
rs1 now contains the value at rs2 + byte_offset
Issue with lb
What to do with the rest of the 24 bits in the 32 bit register?
Sign extend
Or zero extend is lbu (load bit unsigned)
Same idea with loading half words (2 bytes)
Concept of Alignment with memory
Objects must fall on address that is a multiple of their size
How to know if something is Word aligned, Byte Aligned, Half word Aligned
Word aligned: Last hex number is a multiple of 4 (i.e. 0,4,8,C) OR binary representation of last hex number has low 2 bits of 00
Byte aligned: Last hex number can be anything
Half word aligned: Last hex number is a multiple of 2 (0,2,4…,A,C,E) OR binary represnetation of last hex number has low 1 bit of 0
Thing to use to ensure alignment (What section its used in and the order to use it)
.align n - ensures next field is aligned to a multiple of 2^n
Use before doing a .word or .text in .data
Little Endian vs big endian
Example with storing 0×00007ffe
Key point about how they are grouped (based on size of hex)
little endian - stores little end of number first
big endian - stores big end of number first
Size of hex is 4 bits. Since each memory cell is 1 byte in this case, 2 hex values to get 8 bits = 1 byte.
difference between .space and .word (also what section they go in)
Section: .data
.space 4 - allocates 4 bytes, uninitialized
.word 0 - 4 bytes, initialized to 0 (since 0 is int and int has 4 bytes)
In logical operators, numbers for True and False
True - 1
False - 0
What to do instead of a not
How to do not t0, t2
xori with -1 (all 1’s)
xori t0, t2, -1
and can be used to __
mask
Since and with 0 produces 0, with 1 produces original bit…
Spam 0’s for ones you want to mask, spam 1’s for bits you want to isolate
or can be used to ___
force bits to 1
since or with 0 produces original bit, with 1 produces 1…
Spam 0’s for ones you want to keep, spam 1’s for bits you want to convert to 1
Range for immediates
At most 12 bit signed integer
-2^11 —> 2^11 - 1
Meaning, not neccessarily 3 hex numbers. (0×800 —> 0×7FF)
Note for Rars hex values
Rars interprets hex has unsigned. With 32 bits, 0xFFF gets sign extended to 0×00000FFF.
If need 0xFFF as in twos complement -1, need to manually type 0×8 F’s
Logical vs arithmetic shifting
logical - fills empited bits with 0 (doesn’t preserve sign)
arithmetic - fills empied bits by sign extending (preserves sign)
Use case of left and right shifting
Shifting left multiplies original number by 2^n
Shifting right divides original number 2^n
n being the number of bits shifted
Shift syntax (Important note about what is shifted)
Operand rd, rs1, rs2
rs2 - can be register or an immediate value
Shifted by rs2 number of BITS. So if shifting a hex value, essentially moves hex numbers over by rs2/4 positions.
How to know when shift left has overflowed (for signed and unsigned)
Unsigned: First 1 is shifted out of the left edge
Signed: most significant bit changes values
Maximum number of bits that can be loaded with Load Upper Immediate
20 bits
lui t0, 0xFACA
lui t1, 0xFACA0
Difference?
t0 = 0×0FACA000
t1 = 0xFACA0000
Since lui can do 20 bits, 0xFACA gets extended to 0×0FACA.
If statement + syntax (equal to and not equal to)
beq rs1, rs2, label
bne for not equal to
Way to jump directly to label
jal zero, label
2 inequality comparisons in risc-v (with syntax) (signed and unsigned)
blt rs1, rs2, label
bge rs1, rs2, label
To do unsigned comparison…
bltu rs1, rs2, label
Use case of using unsigned comparison
Check IndexoutofBounds errors.
# i = a0, size = a1
bgeu a0, a1, IndexOutofBounds
If i is negative, twos complement so becomes BIG number.
How to for loop
for (int i=0; i < 100; i++)
Using t0 and t1
Put 0 into t0
Put 100 into t1
Define loop label and increment t0
Compare t0 and t1 at end of loop (blt t0, t1, loop)
Array & pointer style for accessing A[i]
Array:
Multiply i by 4 to get 4i
Add base array address by 4i to index
Pointer:
Load base address of the array
In a loop up to the number indexed…
Add 4 to the base address (Or size of whatever the array is holding)
What is the program counter?
Special register that holds memory location of current instruction to execute
What caller and callee uses to jump
Caller: jal ra, function_name
Callee: jalr zero, ra, 0
How to allocate stack on the space
addi sp, sp -4 # Stack goes down
sw register, 0(sp) # register is the register of thing wanting to be stored
How to release space
lw ra, 0(sp) # Assuming sp is pointing at ra
addi sp, sp, 4
jalr zero, ra, 0 # Jumps back to whatever function