Registers and basic arithmetic

ARM assembler in Raspberry Pi – Chapter 2

Registers

At its core, a processor in a computer is nothing but a powerful calculator. Calculations can only be carried using values stored in very tiny memories called registers. The ARM processor in a Raspberry Pi has 16 integer registers and 32 floating point registers. A processor uses these registers to perform integer computations and floating point computations, respectively. We will put floating registers aside for now and eventually we will get back to them in a future installment. Let’s focus on the integer registers.

Those 16 integer registers in ARM have names from r0 to r15. They can hold 32 bits. Of course these 32 bits can encode whatever you want. That said, it is convenient to represent integers in two’s complement as there are instructions which perform computations assuming this encoding. So from now, except noted, we will assume our registers contain integer values encoded in two’s complement.

Not all registers from r0 to r15 are used equally but we will not care about this for now. Just assume what we do is “OK”.

Basic arithmetic

Almost every processor can do some basic arithmetic computations using the integer registers. So do ARM processors. You can ADD two registers. Let’s retake our example from the first chapter.

1
2
3
4
5
6
7
8
/* -- sum01.s */
.global main
 
main:
    mov r1, #3      /* r1 ← 3 */
    mov r2, #4      /* r2 ← 4 */
    add r0, r1, r2  /* r0 ← r1 + r2 */
    bx lr

If we compile and run this program, the error code is, as expected, 7.

$ ./sum01 ; echo $?
7

Nothing prevents us to reuse r0 in a more clever way.

1
2
3
4
5
6
7
8
/* -- sum02.s */
.global main
 
main:
    mov r0, #3      /* r0 ← 3 */
    mov r1, #4      /* r1 ← 4 */
    add r0, r0, r1  /* r0 ← r0 + r1 */
    bx lr

Which behaves as expected.

$ ./sum02 ; echo $?
7

That’s all for today.

Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedIn

, , ,

4 thoughts on “ARM assembler in Raspberry Pi – Chapter 2

  •     <div class="comment-meta commentmetadata"><a href="http://thinkingeek.com/2013/01/10/arm-assembler-raspberry-pi-chapter-2/#comment-1418">
            June 11, 2013 at 7:28 pm</a>        </div>
    
        <p>smart guide … go ahead …</p>
    
        <div class="reply"><a rel="nofollow" class="comment-reply-link" href="http://thinkingeek.com/2013/01/10/arm-assembler-raspberry-pi-chapter-2/?replytocom=1418#respond" onclick="return addComment.moveForm( &quot;div-comment-1418&quot;, &quot;1418&quot;, &quot;respond&quot;, &quot;341&quot; )" aria-label="Reply to Mariani Antonio Mario">Reply</a></div>
                </div>
        </li><!-- #comment-## -->
        <li class="comment odd alt thread-odd thread-alt depth-1 parent" id="comment-183511">
                <div id="div-comment-183511" class="comment-body">
                <div class="comment-author vcard">
            <img alt="" src="http://0.gravatar.com/avatar/0654eb81a0f139a5e30f2d400c10a796?s=54&amp;d=mm&amp;r=g" srcset="http://0.gravatar.com/avatar/0654eb81a0f139a5e30f2d400c10a796?s=64&amp;d=mm&amp;r=g 2x" class="avatar avatar-32 photo grav-hashed grav-hijack" height="32" width="32" id="grav-0654eb81a0f139a5e30f2d400c10a796-0" originals="32" src-orig="http://0.gravatar.com/avatar/0654eb81a0f139a5e30f2d400c10a796?s=32&amp;d=mm&amp;r=g" scale="1.5">            <cite class="fn">Andreas Gwilt</cite> <span class="says">says:</span>        </div>
    
        <div class="comment-meta commentmetadata"><a href="http://thinkingeek.com/2013/01/10/arm-assembler-raspberry-pi-chapter-2/#comment-183511">
            July 18, 2014 at 2:25 pm</a>        </div>
    
        <p>Can’t you just say <code>add r0, r1</code> ?</p>
    
        <div class="reply"><a rel="nofollow" class="comment-reply-link" href="http://thinkingeek.com/2013/01/10/arm-assembler-raspberry-pi-chapter-2/?replytocom=183511#respond" onclick="return addComment.moveForm( &quot;div-comment-183511&quot;, &quot;183511&quot;, &quot;respond&quot;, &quot;341&quot; )" aria-label="Reply to Andreas Gwilt">Reply</a></div>
                </div>
        <ul class="children">
        <li class="comment byuser comment-author-rferrer bypostauthor even depth-2" id="comment-183530">
                <div id="div-comment-183530" class="comment-body">
                <div class="comment-author vcard">
            <img alt="" src="http://1.gravatar.com/avatar/a779b8290b1ca104fdf84d8016fd010b?s=54&amp;d=mm&amp;r=g" srcset="http://1.gravatar.com/avatar/a779b8290b1ca104fdf84d8016fd010b?s=64&amp;d=mm&amp;r=g 2x" class="avatar avatar-32 photo grav-hashed grav-hijack" height="32" width="32" id="grav-a779b8290b1ca104fdf84d8016fd010b-0" originals="32" src-orig="http://1.gravatar.com/avatar/a779b8290b1ca104fdf84d8016fd010b?s=32&amp;d=mm&amp;r=g" scale="1.5">            <cite class="fn">rferrer</cite> <span class="says">says:</span>        </div>
    
        <div class="comment-meta commentmetadata"><a href="http://thinkingeek.com/2013/01/10/arm-assembler-raspberry-pi-chapter-2/#comment-183530">
            July 18, 2014 at 2:57 pm</a>        </div>
    
        <p>Yes, although I think would be less clear for novices.</p>
    

    Kind regards,

        <div class="reply"><a rel="nofollow" class="comment-reply-link" href="http://thinkingeek.com/2013/01/10/arm-assembler-raspberry-pi-chapter-2/?replytocom=183530#respond" onclick="return addComment.moveForm( &quot;div-comment-183530&quot;, &quot;183530&quot;, &quot;respond&quot;, &quot;341&quot; )" aria-label="Reply to rferrer">Reply</a></div>
                </div>
        </li><!-- #comment-## -->
    

    </ul> </li>

  • SaberWriter says:
  •     <div class="comment-meta commentmetadata"><a href="http://thinkingeek.com/2013/01/10/arm-assembler-raspberry-pi-chapter-2/#comment-657258">
            November 24, 2014 at 1:28 am</a>        </div>
    
        <p>This continues to be a great series of articles.  Thanks again for taking the time to write them. Great stuff.</p>
    
        <div class="reply"><a rel="nofollow" class="comment-reply-link" href="http://thinkingeek.com/2013/01/10/arm-assembler-raspberry-pi-chapter-2/?replytocom=657258#respond" onclick="return addComment.moveForm( &quot;div-comment-657258&quot;, &quot;657258&quot;, &quot;respond&quot;, &quot;341&quot; )" aria-label="Reply to SaberWriter">Reply</a></div>
                </div>
        </li><!-- #comment-## -->
        </ul>
    
    <p></p>
        <div id="respond" class="comment-respond">
        <h3 id="reply-title" class="comment-reply-title">Leave a Reply <small><a rel="nofollow" id="cancel-comment-reply-link" href="/2013/01/10/arm-assembler-raspberry-pi-chapter-2/#respond" style="display:none;">Cancel reply</a></small></h3>            <form action="http://thinkingeek.com/wp-comments-post.php" method="post" id="commentform" class="comment-form">
                <p class="comment-notes"><span id="email-notes">Your email address will not be published.</span> Required fields are marked <span class="required">*</span></p><p class="comment-form-comment"><label for="comment">Comment</label> <textarea id="comment" name="comment" cols="45" rows="8" maxlength="65525" aria-required="true" required="required"></textarea></p><p class="comment-form-author"><label for="author">Name <span class="required">*</span></label> <input id="author" name="author" type="text" value="" size="30" maxlength="245" aria-required="true" required="required"></p>
    

    </p>

    </form> </div> </div> </div>