Moduls in ARM assembly question
Posted: Thu Oct 04, 2018 6:10 pm
Hi Guys,
First time posting on the forums. So, my question is about some code I'm using as a tutorial. The reference is from this link: https://thinkingeek.com/2013/01/20/arm- ... -chapter-6 . The C version of the code I fully understand and I should also mention that I under stand what the over goal of the code is. Here is the C version:
Pretty trivial code when viewed in C. Now below is the assembly code:
My question is about line 10 and the 'and' instruction. I understand this is the logical bit wise and. However, I don't understand how it works in this context. I believe this to be the modulo part from the C version of the code. I can't put into words how this is computing to 0 or 1. For example how would and r3, #123, #1 compute to 1? I would greatly appreciate any explanation or link point to an in depth explanation. Thanks in advance!
First time posting on the forums. So, my question is about some code I'm using as a tutorial. The reference is from this link: https://thinkingeek.com/2013/01/20/arm- ... -chapter-6 . The C version of the code I fully understand and I should also mention that I under stand what the over goal of the code is. Here is the C version:
Code: Select all
n = ...;
while(n != 1)
{
if(n % 2 == 0)
n = n / 2;
else
n = 3 * n + 1;
}
Code: Select all
.text
.global main
main:
mov r1, #123 /* r1 ← 123 */
mov r2, #0 /* r2 ← 0 */
loop:
cmp r1, #1 /* compare r1 and 1 */
beq end /* branch to end if r1 == 1 */
and r3, r1, #1 /* r3 ← r1 & 1 */
cmp r3, #0 /* compare r3 and 0 */
bne odd /* branch to odd if r3 != 0 */
even:
mov r1, r1, ASR #1 /* r1 ← (r1 >> 1) */
b end_loop
odd:
add r1, r1, r1, LSL #1 /* r1 ← r1 + (r1 << 1) */
add r1, r1, #1 /* r1 ← r1 + 1 */
end_loop:
add r2, r2, #1 /* r2 ← r2 + 1 */
b loop /* branch to loop */
end:
mov r0, r2
bx lr