Consider the given -code and its corresponding assembly code, with a few operands being unknown. Some useful information as well as the semantics of each unique assembly instruction is annotated as inline comments in the code. The memory is byte-addressable.
//C-code int a[10], b[10], i; // int is 32 bit for(i=0; i<10; i++) a[i] = b[i] * 8; | ;assembly code (; indicates comments) ;r1-r5 are 32-bit integer registers ;initialize r1=0, r2=10 ;initialize r3, r4 with base address of a, b L01: jeq r1, r2, end ;if(r1==r2) goto end L02: lw, r5, 0(r4) ;r5 <- Memory[r4+0] L03: shl, r5, r5, U1 ;r5 <- r5 << U1 L04: sw, r5, 0(r3) ;Memory[r3+0] <- r5 L05: add, r3, r3, U2 ;r3 <- r3+U2 L06: add, r4, r4, U3 L07: add, r1, r1, 1 L08: jmp U4 ;goto U4 L09: end |
Which one of the following options is a replacement for operands in the position in the above assembly code?
View worked solution
Answer: B
To find the correct replacements for the operands , we analyze the assembly instructions corresponding to the C-code loop:
-
Determining :
The C-code performs the multiplication
a[i] = b[i] * 8. In binary arithmetic, multiplying an integer by is equivalent to a logical left shift by bits (since ).The assembly instruction at
L03is:shl, r5, r5, U1r5 <- r5 << U1Thus, we must have .
-
Determining and :
The arrays
aandbconsist of 32-bit (4-byte) integers. Since the memory is byte-addressable, moving to the next element in the array (i.e., from indexitoi+1) requires incrementing the memory address by bytes.The instructions at
L05andL06update the pointersr3(for arraya) andr4(for arrayb):add, r3, r3, U2add, r4, r4, U3
-
Determining :
At the end of the loop body (at
L08), the control must jump back to the loop condition check to evaluate whether the loop should continue.The loop condition check is located at label
L01(jeq r1, r2, end).Thus, the jump target is .
Combining these values, we get:
Free practice · Your answers on this page are not saved.
