This is in SPARC Assembly which is RISC. Please do not do it in other assembly l
ID: 3849123 • Letter: T
Question
This is in SPARC Assembly which is RISC. Please do not do it in other assembly languages because it will confuse me. I am somewhat familar with ARM though. If possible, could you draw a memory stack so that I can see how the local varibles are being allocated on the stack? I am somewhat confused about how the struct is being allocated and what order they should be allocated.
6. Local Variables, The Stack, and Return Values Here is a C function that allocates a few local variables, performs some assignments and returns a value. Don't worry about any local variables not being initialized before being used. Just do a direct translation. Draw lines. char Draw a line between each group of assembly statements representing fubar int a, char b each C statement and label them 1, 2, 3, or 4 appropriately. int local stack varl Keep the formal parameters a and b in their incoming registers int local stack var2; struct foo int s1 int s213 char s313 char s 4; local stack var3 local stack var 3.s ++b 1 local stack varl ocal stack var2 local stack var 3. s ocal stack var3.s2 0 a++; 3 return local stack var 3.s3 21 Write the equivalent full unoptimized SPARC assembly language module to perform the equivalent. You must allocate all local variables on the stack. No short cuts. Treat each statement independently. (18 points)Explanation / Answer
Answer: See the corresponding ARM assembly code below:
--------------------------------------
fubar(int, char):
str fp, [sp, #-4]!
add fp, sp, #0
sub sp, sp, #44
str r0, [fp, #-40]
mov r3, r1
strb r3, [fp, #-41]
ldrb r3, [fp, #-41]
add r3, r3, #1
strb r3, [fp, #-41]
ldrb r3, [fp, #-41]
strb r3, [fp, #-13]
ldr r3, [fp, #-8]
ldr r3, [r3]
add r2, r3, #1
ldr r3, [fp, #-8]
str r2, [r3]
ldr r3, [fp, #-8]
ldr r3, [r3]
str r3, [fp, #-12]
ldr r2, [fp, #-28]
ldr r3, [fp, #-40]
add r3, r3, #1
str r3, [fp, #-40]
ldr r3, [fp, #-40]
add r3, r2, r3
str r3, [fp, #-32]
ldrb r3, [fp, #-14] @ zero_extendqisi2
mov r0, r3
sub sp, fp, #0
ldr fp, [sp], #4
bx lr
-----------------------------------------
Explanation: In this code, fp stands for frame pointer, while sp stands for stack pointer. Stack pointer points to the top of (memory) stack (free). Frame pointer is used mainly during function/subroutine calls. It points to the start of stack frame and doesn't change as long as a subroutine is not finished. In this way, parameters passed to routine doesn't change offset relative to frame pointer. Variables inside the function are created and alloted space in the order in which they are encountered.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.