Machine-translated by Google. Questions are authored in English.
Top

Discussion

Consider the given C-code and its corresponding assembly code, with a few operands U1-U4 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\(\lt\)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 \(\lt\)- Memory[r4+0]
L03: shl r5, r5, U1 ;r5 \(\lt\)- r5 \(\lt\lt\) U1
L04: sw r5, 0(r3) ;Memory[r3+0] \(\lt\)- r5
L05: add r3, r3, U2 ;r3 \(\lt\)- 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 CORRECT replacement for operands in the position (U1, U2, U3, U4) in the above assembly code?

  • A.(8, 4, 1, L02)
  • B.(3, 4, 4, L01)
  • C.(8, 1, 1, L02)
  • D.(3, 1, 1, L01)

Answer: B

The operands are (3, 4, 4, L01).

  • U1: multiplying by 8 is a left shift by 3 bits, since \(8 = 2^{3}\). A shift of 8 would multiply by 256.
  • U2 and U3: an int is 32 bits, 4 bytes, and memory is byte-addressable, so the pointers r3 (into a) and r4 (into b) must each advance by 4 to reach the next element.
  • U4: after r1 is incremented, the loop must test r1 == r2 again before the next iteration, so it jumps back to L01. Jumping to L02 skips the test and never leaves the loop.

(8, 4, 1, L02) gets the shift, the b pointer and the jump wrong; (8, 1, 1, L02) also steps the pointers by a single byte; and (3, 1, 1, L01) has the right shift and jump but moves only one byte per element, reading and writing overlapping parts of the integers.

No comment is present. Be the first to comment.
Loading…

Post your comment

Please log in or register to post a comment.