The operating system

ARM assembler in Raspberry Pi – Chapter 19

So far our small assembler programs have output messages using printf and some of them have read input using scanf. These two functions are implemented in the C library, so they are more or less supported in any environment supporting the C language. But how does a program actually communicate with the world?

The operating system

Our Raspberry Pi runs Raspbian. Raspbian is an operating system based on Debian on top of the Linux kernel. The operating system is a piece of software (usually a collection of pieces that together form a useful system) that enables and manages the resources required by programs to run. Which sort of resources, you may be wondering? Well, many different kinds of them: processes, files, network devices, network communications, screens, printers, terminals, timers, etc.

From the point of view of the program, the operating system is just a big servant providing lots of services to the program. But the operating system is also a caretaker, taking action when something goes wrong or programs (sometimes caused by the users of the operating system) attempt to do something that they are not authorized to do. In our case, Linux is the kernel of the Raspbian operating system. The kernel provides the most basic functionality needed to provide these services (sometimes it provides them directly, sometimes it just provides the minimal essential functionality so they can be implemented). It can be viewed as a foundational program that it is always running (or at least, always ready) so it can serve the requests of the programs run by the users. Linux is a UNIX®-like kernel and as such shares lots of features with the long lineage of UNIX®-like operating systems.

Processes

In order to assign resources, the operating system needs an entity to which grant such resources. This entity is called a process. A process is a running program. The same program may be run several times, each time it is run it is a different process.

System calls

A process interacts with the operating system by performing system calls. A system call is conceptually like calling a function but more sophisticated. It is more sophisticated because now we need to satisfy some extra security requirements. An operating system is a critical part of a system and we cannot let processes dodge the operating system control. A usual function call offers no protection of any kind. Any strategy we could design on top of a plain function call would easily be possible to circumvent. As a consequence of this constraint, we need support from the architecture (in our case ARM) in order to safely and securely implement a system call mechanism.

In Linux ARM we can perform a system call by using the instruction swi. This instruction means software interruption and its sole purpose is to make a system call to the operating system. It receives a 24-bit operand that is not used at all by the processor but could be used by the the operating system to tell which service has been requested. In Linux such approach is not used and a 0 is set as the operand instead. So, in summary, in Linux we will always use swi #0 to perform a system call.

An operating system, and particularly Linux, provides lots of services through system calls so we need a way to select one of them. We will do this using the register r7. System calls are similar to function calls in that they receive parameters. No system call in Linux receives more than 7 arguments and the arguments are passed in registers r0 to r6. If the system call returns some value it will be returned in register r0.

Note that the system call convention is incompatible with the convention defined by the AAPCS, so programs will need specific code that deals with a system call. In particular, it makes sense to wrap these system calls into normal functions, that externally, i.e. from the point of the caller, follow the AAPCS. This is precisely the main purpose of the C library. In Linux, the C library is usually GNU Libc (but others can be used in Linux). These libraries hide the extra complexity of making system calls under the appearance of a normal function call.

Hello world, the system call way

As a simple illustration of calling the operating system we will write the archetypical “Hello world” program using system calls. In this case we will call the function write. Write receives three parameters: a file descriptor where we will write some data, a pointer to the data that will be written and the size of such data. Of these three, the most obscure may be now the file descriptor. Without entering into much details, it is just a number that identifies a file assigned to the process. Processes usually start with three preassigned files: the standard input, with the number 0, the standard output, with the number 1, and the standard error, with the number 2. We will write our messages to the standard output, so we will use the file descriptor 1.

The “ea-C” way

Continuing with our example, first we will call write through the C library. The C library follows the AAPCS convention. The prototype of the write system call can be found in the Linux man pages and is as follows.

ssize_t write(int fd, const void *buf, size_t count);

Here both size_t and ssize_t are 32-bit integers, where the former is unsigned and the latter signed. Equipped with our knowledge of the AAPCS and ARM assembler it should not be hard for us to perform a call like the following

const char greeting[13] = "Hello world\n";
write(1, greeting, sizeof(greeting)); // Here sizeof(greeting) is 13

Here is the code

/* write_c.s */
 
.data
 
greeting: .asciz "Hello world\n"
after_greeting:
 
/* This is an assembler constant: the assembler will compute it. Needless to say
   that this must evaluate to a constant value. In this case we are computing the
   difference of addresses between the address after_greeting and greeting. In this
   case it will be 13 */
.set size_of_greeting, after_greeting - greeting
 
.text
 
.globl main
 
main:
    push {r4, lr}
 
    /* Prepare the call to write */  
    mov r0, #1                /* First argument: 1 */
    ldr r1, addr_of_greeting  /* Second argument: &greeting */
    mov r2, #size_of_greeting /* Third argument: sizeof(greeting) */
    bl write                  /* write(1, greeting, sizeof(greeting));
 
    mov r0, #0
    pop {r4, lr}
    bx lr
 
addr_of_greeting : .word greeting

.data

greeting: .asciz "Hello world\n" after_greeting:

/* This is an assembler constant: the assembler will compute it. Needless to say that this must evaluate to a constant value. In this case we are computing the difference of addresses between the address after_greeting and greeting. In this case it will be 13 */ .set size_of_greeting, after_greeting - greeting

.text

.globl main

main: push {r4, lr}

/* Prepare the call to write */  
mov r0, #1                /* First argument: 1 */
ldr r1, addr_of_greeting  /* Second argument: &greeting */
mov r2, #size_of_greeting /* Third argument: sizeof(greeting) */
bl write                  /* write(1, greeting, sizeof(greeting));

mov r0, #0
pop {r4, lr}
bx lr

addr_of_greeting : .word greeting

The system call way

Ok, calling the system call through the C library was not harder than calling a normal function. Let’s try the same directly performing a Linux system call. First we have to identify the number of the system call and put it in r7. The call write has the number 4 (you can see the numbers in the file /usr/include/arm-linux-gnueabihf/asm/unistd.h). The parameters are usually the same as in the C function, so we will use registers r0, r1 and r2 likewise.

/* write_sys.s */
 
.data
 
 
greeting: .asciz "Hello world\n"
after_greeting:
 
.set size_of_greeting, after_greeting - greeting
 
.text
 
.globl main
 
main:
    push {r7, lr}
 
    /* Prepare the system call */
    mov r0, #1                  /* r0 ← 1 */
    ldr r1, addr_of_greeting    /* r1 ← &greeting */
    mov r2, #size_of_greeting   /* r2 ← sizeof(greeting) */
 
    mov r7, #4                  /* select system call 'write' */
    swi #0                      /* perform the system call */
 
    mov r0, #0
    pop {r7, lr}
    bx lr
 
addr_of_greeting : .word greeting

.data

greeting: .asciz "Hello world\n" after_greeting:

.set size_of_greeting, after_greeting - greeting

.text

.globl main

main: push {r7, lr}

/* Prepare the system call */
mov r0, #1                  /* r0 ← 1 */
ldr r1, addr_of_greeting    /* r1 ← &greeting */
mov r2, #size_of_greeting   /* r2 ← sizeof(greeting) */

mov r7, #4                  /* select system call 'write' */
swi #0                      /* perform the system call */

mov r0, #0
pop {r7, lr}
bx lr

addr_of_greeting : .word greeting

As you can see it is not that different to a function call but instead of branching to a specific address of code using bl we use swi #0. Truth be told, it is rather unusual to perform system calls directly. It is almost always preferable to call the C library instead.

That’s all for today.

Share on Facebook Share on Google+ Tweet about this on Twitter Share on LinkedIn

		<h3>10 thoughts on “<span>ARM assembler in Raspberry Pi – Chapter 19</span>”</h3>
	<ul class="commentlist">
			<li class="comment even thread-even depth-1 parent" id="comment-172790">
			<div id="div-comment-172790" class="comment-body">
			<div class="comment-author vcard">
		<img alt="" src="http://0.gravatar.com/avatar/9188c3fc6f4eb57e20b2cf3237f01bf7?s=54&amp;d=mm&amp;r=g" srcset="http://0.gravatar.com/avatar/9188c3fc6f4eb57e20b2cf3237f01bf7?s=64&amp;d=mm&amp;r=g 2x" class="avatar avatar-32 photo grav-hashed grav-hijack" height="32" width="32" id="grav-9188c3fc6f4eb57e20b2cf3237f01bf7-0" originals="32" src-orig="http://0.gravatar.com/avatar/9188c3fc6f4eb57e20b2cf3237f01bf7?s=32&amp;d=mm&amp;r=g" scale="1.5">			<cite class="fn">Guojian</cite> <span class="says">says:</span>		</div>
	
	<div class="comment-meta commentmetadata"><a href="http://thinkingeek.com/2014/05/24/arm-assembler-raspberry-pi-chapter-19/#comment-172790">
		July 4, 2014 at 11:04 pm</a>		</div>

	<p>this is really great, I learned a lot from this serial. now would you continue to write something about the stack information for a system call? how a system call function allocate stack for itself ?</p>

	<div class="reply"><a rel="nofollow" class="comment-reply-link" href="http://thinkingeek.com/2014/05/24/arm-assembler-raspberry-pi-chapter-19/?replytocom=172790#respond" onclick="return addComment.moveForm( &quot;div-comment-172790&quot;, &quot;172790&quot;, &quot;respond&quot;, &quot;1518&quot; )" aria-label="Reply to Guojian">Reply</a></div>
			</div>
	<ul class="children">
	<li class="comment byuser comment-author-rferrer bypostauthor odd alt depth-2 parent" id="comment-175299">
			<div id="div-comment-175299" 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/2014/05/24/arm-assembler-raspberry-pi-chapter-19/#comment-175299">
		July 8, 2014 at 9:34 am</a>		</div>

	<p>Hi Guojian,</p>

while the precise stack construction inside the operating system is related to assembler, it is closer to a systems programming topic, which I am not going to cover in these tutorials.

Kind regards

	<div class="reply"><a rel="nofollow" class="comment-reply-link" href="http://thinkingeek.com/2014/05/24/arm-assembler-raspberry-pi-chapter-19/?replytocom=175299#respond" onclick="return addComment.moveForm( &quot;div-comment-175299&quot;, &quot;175299&quot;, &quot;respond&quot;, &quot;1518&quot; )" aria-label="Reply to rferrer">Reply</a></div>
			</div>
	<ul class="children">
	<li class="comment even depth-3 parent" id="comment-205116">
			<div id="div-comment-205116" class="comment-body">
			<div class="comment-author vcard">
		<img alt="" src="http://2.gravatar.com/avatar/e1d78cf1c6802a32ecb8f36aeff2591b?s=54&amp;d=mm&amp;r=g" srcset="http://2.gravatar.com/avatar/e1d78cf1c6802a32ecb8f36aeff2591b?s=64&amp;d=mm&amp;r=g 2x" class="avatar avatar-32 photo grav-hashed grav-hijack" height="32" width="32" id="grav-e1d78cf1c6802a32ecb8f36aeff2591b-0" originals="32" src-orig="http://2.gravatar.com/avatar/e1d78cf1c6802a32ecb8f36aeff2591b?s=32&amp;d=mm&amp;r=g" scale="1.5">			<cite class="fn">dummys</cite> <span class="says">says:</span>		</div>
	
	<div class="comment-meta commentmetadata"><a href="http://thinkingeek.com/2014/05/24/arm-assembler-raspberry-pi-chapter-19/#comment-205116">
		August 8, 2014 at 8:32 am</a>		</div>

	<p>Hi, I’ve learned so many cool things with your tutorial, may you continue on how we can interact with the hardware component of the raspberry pi ?<br>

Thanks
Keep the good works
dummys

	<div class="reply"><a rel="nofollow" class="comment-reply-link" href="http://thinkingeek.com/2014/05/24/arm-assembler-raspberry-pi-chapter-19/?replytocom=205116#respond" onclick="return addComment.moveForm( &quot;div-comment-205116&quot;, &quot;205116&quot;, &quot;respond&quot;, &quot;1518&quot; )" aria-label="Reply to dummys">Reply</a></div>
			</div>
	<ul class="children">
	<li class="comment byuser comment-author-rferrer bypostauthor odd alt depth-4" id="comment-217536">
			<div id="div-comment-217536" 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-1" 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/2014/05/24/arm-assembler-raspberry-pi-chapter-19/#comment-217536">
		August 20, 2014 at 9:54 am</a>		</div>

	<p>Hi dummys,</p>

my intent was to gently introduce assembler, rather than interacting with hardware.

In my opinion it is much better to do this with a higher level language, even if it is C.

Kind regards,

	<div class="reply"><a rel="nofollow" class="comment-reply-link" href="http://thinkingeek.com/2014/05/24/arm-assembler-raspberry-pi-chapter-19/?replytocom=217536#respond" onclick="return addComment.moveForm( &quot;div-comment-217536&quot;, &quot;217536&quot;, &quot;respond&quot;, &quot;1518&quot; )" aria-label="Reply to rferrer">Reply</a></div>
			</div>
	</li><!-- #comment-## -->
  • claes says:
    	<div class="comment-meta commentmetadata"><a href="http://thinkingeek.com/2014/05/24/arm-assembler-raspberry-pi-chapter-19/#comment-226103">
    		August 27, 2014 at 9:13 pm</a>		</div>
    
    	<p>I think this lesson, the calling convention with “swi” was very good.</p>
    

    In the last example, shouldn’t it be register r7 that is pushed and poped instead of r4?

    	<div class="reply"><a rel="nofollow" class="comment-reply-link" href="http://thinkingeek.com/2014/05/24/arm-assembler-raspberry-pi-chapter-19/?replytocom=226103#respond" onclick="return addComment.moveForm( &quot;div-comment-226103&quot;, &quot;226103&quot;, &quot;respond&quot;, &quot;1518&quot; )" aria-label="Reply to claes">Reply</a></div>
    			</div>
    	<ul class="children">
    	<li class="comment byuser comment-author-rferrer bypostauthor odd alt depth-2" id="comment-226138">
    			<div id="div-comment-226138" 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-2" 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/2014/05/24/arm-assembler-raspberry-pi-chapter-19/#comment-226138">
    		August 27, 2014 at 9:52 pm</a>		</div>
    
    	<p>Hi claes,</p>
    

    yes, you are right. r7 is a callee-saved register so we must preserve its value upon returning the function. I already updated the code.

    Thanks a lot,

    	<div class="reply"><a rel="nofollow" class="comment-reply-link" href="http://thinkingeek.com/2014/05/24/arm-assembler-raspberry-pi-chapter-19/?replytocom=226138#respond" onclick="return addComment.moveForm( &quot;div-comment-226138&quot;, &quot;226138&quot;, &quot;respond&quot;, &quot;1518&quot; )" aria-label="Reply to rferrer">Reply</a></div>
    			</div>
    	</li><!-- #comment-## -->
    
  • 	<div class="comment-meta commentmetadata"><a href="http://thinkingeek.com/2014/05/24/arm-assembler-raspberry-pi-chapter-19/#comment-817231">
    		March 11, 2015 at 2:14 am</a>		</div>
    
    	<p>I see that SoftWare Interrupt (SWI) is now replaced by SerVice Call (SVC) in ARM documentation but works the same and the old name is still accepted. I’d really like to find detailed information of what should be in the registers for READ (3), WRITE (4), OPEN (5), CLOSE (6), CREAT (7), etc. Since it’s OS dependent, where do I look?</p>
    
    	<div class="reply"><a rel="nofollow" class="comment-reply-link" href="http://thinkingeek.com/2014/05/24/arm-assembler-raspberry-pi-chapter-19/?replytocom=817231#respond" onclick="return addComment.moveForm( &quot;div-comment-817231&quot;, &quot;817231&quot;, &quot;respond&quot;, &quot;1518&quot; )" aria-label="Reply to William Pervin">Reply</a></div>
    			</div>
    	<ul class="children">
    	<li class="comment byuser comment-author-rferrer bypostauthor odd alt depth-2" id="comment-818009">
    			<div id="div-comment-818009" 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-3" 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/2014/05/24/arm-assembler-raspberry-pi-chapter-19/#comment-818009">
    		March 11, 2015 at 8:47 pm</a>		</div>
    
    	<p>Hi William,</p>
    

    this is indeed OS dependent. In the case of Raspberry Pi under Raspbian, the underlying operating system is a modified Debian with a Linux kernel.

    You can see the prototypes (in C) of the Linux system calls in the Linux Cross Reference in the following URL: http://lxr.free-electrons.com/source/include/linux/syscalls.h

    Kind regards,

    	<div class="reply"><a rel="nofollow" class="comment-reply-link" href="http://thinkingeek.com/2014/05/24/arm-assembler-raspberry-pi-chapter-19/?replytocom=818009#respond" onclick="return addComment.moveForm( &quot;div-comment-818009&quot;, &quot;818009&quot;, &quot;respond&quot;, &quot;1518&quot; )" aria-label="Reply to rferrer">Reply</a></div>
    			</div>
    	</li><!-- #comment-## -->
    
  • Denis says:
    	<div class="comment-meta commentmetadata"><a href="http://thinkingeek.com/2014/05/24/arm-assembler-raspberry-pi-chapter-19/#comment-981580">
    		December 10, 2016 at 11:52 am</a>		</div>
    
    	<p>ok. but i did’t understand how can i output content of registet to terminal.</p>
    
    	<div class="reply"><a rel="nofollow" class="comment-reply-link" href="http://thinkingeek.com/2014/05/24/arm-assembler-raspberry-pi-chapter-19/?replytocom=981580#respond" onclick="return addComment.moveForm( &quot;div-comment-981580&quot;, &quot;981580&quot;, &quot;respond&quot;, &quot;1518&quot; )" aria-label="Reply to Denis">Reply</a></div>
    			</div>
    	<ul class="children">
    	<li class="comment byuser comment-author-rferrer bypostauthor odd alt depth-2" id="comment-981866">
    			<div id="div-comment-981866" 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-4" originals="32" src-orig="http://1.gravatar.com/avatar/a779b8290b1ca104fdf84d8016fd010b?s=32&amp;d=mm&amp;r=g" scale="1.5">			<cite class="fn">Roger Ferrer Ibáñez</cite> <span class="says">says:</span>		</div>
    	
    	<div class="comment-meta commentmetadata"><a href="http://thinkingeek.com/2014/05/24/arm-assembler-raspberry-pi-chapter-19/#comment-981866">
    		December 15, 2016 at 9:21 pm</a>		</div>
    
    	<p>Ho Denis,</p>
    

    calling printf or a similar function should do. In other chapters we have called printf. You may want to revisit them.

    Kind regards,

    	<div class="reply"><a rel="nofollow" class="comment-reply-link" href="http://thinkingeek.com/2014/05/24/arm-assembler-raspberry-pi-chapter-19/?replytocom=981866#respond" onclick="return addComment.moveForm( &quot;div-comment-981866&quot;, &quot;981866&quot;, &quot;respond&quot;, &quot;1518&quot; )" aria-label="Reply to Roger Ferrer Ibáñez">Reply</a></div>
    			</div>
    	</li><!-- #comment-## -->
    
  • <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="/2014/05/24/arm-assembler-raspberry-pi-chapter-19/#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>
    


    书籍推荐