Lecture 4 (B) - ARM Architecture - Part 2
Lecture 4 (B) - ARM Architecture - Part 2
Department of CSE
2024-25 Even semester
Contents
• Decoding (Hex to Assembly)
• Condition codes
• Procedures
• B L1
• ADD X10,X11,X12 //Skipped
• L1 : SUBX10,X11,X12
X9 X9
1 0
If (x == 0)
f = f + 20
else if (x == 1)
f=f–1
else
f=f+1
• L1
• SUBI X10, X10, #1
• B EXIT
• L2:
• ADDI X10, X10, #20
• Exit:
while (True){
k = k + 1;
}
Compiled LEGv8 code:
• Loop: ADDI X24, X24, #1
• B Loop
•Overflow (V):
•Set to 1 when the signed result of an addition or subtraction exceeds the range
of the signed number representation.
•For example, adding two positive numbers results in a negative, or adding two
negatives results in a positive.
a in X22, b in X23
Exit:
Exit:
• while (i < 100) { // Loop condition: i < 100 (CMPI X10, 100 and B.LT LOOP)
• result += MemArray[i]; // Add MemArray[i] to result (LDUR X11 and ADD X0, X0,
X11)
• i++; // Increment i (ADDI X10, X10, #1)
•}
do {
int value = *(int *)X1; // Load the value from memory at address X1
result += value; // Add the value to result
X1 += 8; // Increment the memory address by 8 bytes (size of int in a 64-bit system)
i++; // Increment the loop counter i
} while (i < 100); // Check if i (X10) is less than 100
Step 1: Save registers X19, temporary registers (X9 and X10) into stack
// adjust stack to make room for 3 items
SUBI SP, SP, #24
STUR X10, [SP,#16]
STUR X9, [SP,#8]
STUR X19, [SP,#0]
LDUR X9, [SP,#8] // restore register X9 for caller Procedure argument/ result X0-X7
LDUR X10, [SP,#16] // restore register X10 for caller Link Register (X30): Must be preserved if your function calls other
ADDI SP,SP,#24 // adjust stack to delete 3 items functions.
// Step 4: Restore stack frame If you need extra storage for intermediate
Temporary Registers (Optional):
BR LR // branch back to calling routine values (e.g., when you're running out of registers), you can save
temporary registers (X9–X15) to the stack and restore them later.
ADD X9,X0,X1
MUL X10,X2,X3
SUB X19,X9,X10
ADD X0,X19,XZR
LDUR X19,[SP,#0]
ADDI SP,SP,#8
BR LR
modified_example:
ADD X9, X0, X1
MUL X10, X2, X3
MUL X11, X9, X10
UDIV X12,X11,#10
UDIV X13,X12,#7
SUBI SP, SP, #8
STUR X19, [SP,#0]
LSR X14, X11, #2
SUB X14, X13, X14
AND X19, X14, 0XFF
ADD X0,X19,XZR
LDUR X19,[SP,#0]
ADDI SP,SP,#8
BR LR
• Argument n in X0 (Argument)
• Result in X1
Step 5: Next, the value register X1 gets the product of old argument X0 and the
current value of the value register.
• MUL X1,X0,X1 // return n * fact (n − 1)
• The stack is adjusted to make room for all the saved registers and any
memory-resident local variables.
• Stable frame pointer
• If there are no local variables on the stack within a procedure, the compiler
will save time by not setting and restoring the frame pointer.
• When a frame pointer is used, it is initialized using the address in SP on a
call, and SP is restored using FP.
23-01-2025 Dept of CSE, Amrita School of Computing, Coimbatore 75
Working of SP and FP:
• When a function is called:
• The current SP is pushed onto the
stack.
• The value of the current FP is also
pushed onto the stack.
• The SP is updated to point to the new
stack frame.
• The FP is set to the current SP value,
marking the beginning of the current
function's stack frame