Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

A student writes the following C++ statement. while (b[j] ! = x) j ++; Assume th

ID: 3582665 • Letter: A

Question

A student writes the following C++ statement. while (b[j] ! = x) j ++; Assume the variables j and x are stored into registers r2 and r4, and the base address of the array b is in register r0, the above C++ statement is compiled into a program fragment of ARM instructions as below. Loop: ADD ...; CMP ...; B Loop; Exit: Complete the above program fragment. Besides, clearly show the required modification(s) to the complete program fragment when the original C++ statement is changed to: while (b[j] == x) j++;

Explanation / Answer

Arm assembly code as per  arm-none-eabi-g++ (GNU Tools for ARM Embedded Processors) 5.4.1 20160919 (release) specification.


.LOOP:
ldr r3, .L5
ldr r3, [r3]
ldr r2, .L5+4
ldr r2, [r2, r3, asl #2]
ldr r3, .L5+8
ldr r3, [r3]
cmp r2, r3
beq .EXIT // ( IF while(b[j] == x) j++; then this line will be bne .EXIT
ldr r3, .L5
ldr r3, [r3]
add r3, r3, #1
ldr r2, .L5
str r3, [r2]
b .LOOP
.EXIT:
mov r0, r0 @ nop
bx lr
.L5:
.word x
.word b
.word y