What are the values of the registers, as decimal integers, after executing the following code?
mov r0, #9 mov r1, #1 top: cmp r0, #0 beq finish lsl r1, r1, #1 sub r0, r0, #1 b top finish: mov r2, #47
R0: R1: R2: R3:
Answer is as follows :
mov r0, #9 // move data 9 to register r0. So r0 contains 9
mov r1, #1 // move data 1 to register r1. So r1 contains 1
top:
cmp r0, #0 // Compare value of r0 with 0 i.e. 9 with 0 (not equal)
beq finish //if both values are equal, than jump to label finish. Both are not equal
lsl r1, r1, #1 // logical shift the contents of r1 i.e. 1 in decimal or 0001 in binary after performing shift left we get 0010 i.e. 2. So r1 contains 2 now
sub r0, r0, #1 // perform r0 = r0-1 i.e. r0 = 9-1 = 8
b top // branch to top
finish:
mov r2, #47
So at first r0 = 8 , r1 = 2
Again branch to label top when r0= 8:
Both values are not equal again, so perform the operations.
lsl r1, r1, #1 // logical shift the contents of r1 i.e. 2 in decimal or 0010 in binary after performing shift left we get 0100 i.e. 4. So r1 contains 4 now
sub r0, r0, #1 // perform r0 = r0-1 i.e. r0 = 8-1 = 7
b top // branch to top
So now r0 = 7 , r1 = 4
By doing these calculations
we get when r0 = 7 ,
r1 = 1000 (after shifting) i.e. 8, r0 = 6
we get when r0 = 6
r1 = 10000 (after shifting) i.e. 16, r0 = 5
we get when r0 = 5
r1 = 100000 (after shifting) i.e. 32, r0 = 4
we get when r0 = 4
r1 = 1000000 (after shifting) i.e. 64, r0 = 3
we get when r0 = 3
r1 = 10000000 (after shifting) i.e. 128, r0 = 2
we get when r0 = 2
r1 = 100000000 (after shifting) i.e. 257, r0 = 1
we get when r0 = 1
r1 = 1000000000 (after shifting) i.e. 513, r0 = 0
we get when r0 = 0
So values get equal and control jumps to label finish
finish:
mov r2, #47 //move data 47 to register r2. So r2 contains 47.
SO AT LAST WE GET :
r0 = 0 , r1 = 513 , r2 = 47
There is no update in register r3. So it contains default value
All the registers are assumed to be 16 bit.
if there is any query please ask in comments..
Get Answers For Free
Most questions answered within 1 hours.