Question:
Problem with assembly code, need advice plz?
Wayne
2008-02-05 12:23:51 UTC
HI in an introduction to assembly code course I ran into the following pb:
"Consider the following segment of ARM assembly language instructions. Describe in
words when r0 is cleared.
MVN r2, #128
BIC r5, r5, r2
CMP r5, #0
BNE DONE
AND r0, r0, #0
DONE SUB r2, r3, #20"
If I give a value to r5 and simulate the code on paper, I just don't get what's the purpose of the line containing r0. And I don't see what's expected of me.
Any advice ?
Thx
Three answers:
dogsafire
2008-02-05 13:09:17 UTC
The first response you got is correct, but there is one catch. r0 is cleared only if the code does not branch to DONE.



So what must happen is that r5 must contain a zero (because of the comparison and subsequent branch instructions), then r0 will be cleared.



I am not familiar with this processor's language, but this is what I understand from the above code.



The first instruction is a MoVe Negative, so the number 128 (10000000 in binary) is negated and placed in r2. r2 now contains 01111111.



The BIt Clear operation negates the contents of r2 (which changes it back to 10000000) and ANDs that with the contents of r5.



The CoMPare determines whether r5 is a zero

r5 would be zero if it contained any number other than one with a high order bit value of 10000000. In other words, if r5 contains a number less than 128, the CMP instruction will find that r5 contains a zero (because the BIC instruction cleared all the low order bits).



If r5 contains a zero, then the Branch Not Equal instruction will not execute and r0 will be set to zero.



It has been a long time since I dabbled with assembly of any kind, so don't just copy what I said. Read the processor instructions and follow along with my example. I may have made a mistake, but you should be able to correct it easily.



Basically, just follow through the instructions and see under what conditions the AND statement executes
indiantrumpet
2008-02-05 12:40:07 UTC
I would need more background on this particular program. What do the subroutine's mean? Where and what are they defined. It is hard to ask what is what when all you have is a small part of the code. Looking at what you have though, the BNE is normally a branch not equal command that is implemented to prevent squashing of commands. Normally, the command right after BNE is guaranteed to execute before jumping back or breaking from the loop. What this command is doing is that it is storing r0 with the data from ANDing 000 and r0's original content. Meaning that when you AND anything with 0 values, you pretty much clear its memory. I hope this helps.
2008-02-05 12:35:49 UTC
You take r0, and it with an immediate value of 0, which gives always 0 and then write the resulting 0 back to r0, I suppose. In other words... it sets r0 to 0. Just guessing here... but a lot of CPUs use that for clearing a register. It saves a special instruction for that purpose.



Not sure what this piece of code needs that for, but if it is a section of a larger piece of code, it might make perfect sense.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...