Electronic – Assembly Language Program Design


This is a homework assignment problem and I tried to solve this all last night but I am still a newbie in Assembly language.

Dont give me the full solution, just give me a hint.

Design an ARM Assembly Language program that will examine a 32-bit
value stored in R1 and count the number of contiguous sequences of 1s.
For example, the value: 01110001000111101100011100011111 contains six
sequences of 1s.

Write the final value in register, R2.

Now, the algorithm I think is to read each character one by one, and increase i by 1 everytime it faces 2 continuous 1s. But how to do it in Assembly Language?

Best Answer

Write a loop to shift the word left or right (doesn't matter) until the word is all zeros. The carry flag gives you the value of the next bit.

If carry is 1 and the previous carry was zero you're starting a new sequence of 1s. Then set a previous-was-a-first-one flag. (I assume that by contiguous you mean at least 2.)

If carry is 1 and previous-was-a-first-one is set you have a contiguous series, and increment your counter. Clear the previous-was-a-first-one flag.

If carry is 0 then clear the previous-was-a-first-one.

Apparently "contiguous" doesn't require more than 1 bit, and then it's even simpler:

Set `previous' to `0`.
Shift left until word is all zeros.
If `carry` = `1` and `previous` = `0` increment counter.
Set `previous` to `carry`.
Related Topic