Monday 6 October 2014

SPO600: Lab3 - Loop in Assembly

In lab3, we were asked to write the assembly programs in both x86_64 and aarch64 to display number 0-29 in a loop.

This was my first assembly code. And it took me one night and 2 whole days to get them work. Now the loop is working to display the numbers. But I still have the problem for the newlines. My outputs do not have the newlines as I expect.  In other words, my output is on one line. I spent some time on this issue, but have not found the solution.

One frustrating thing in assembly is that different platform requires different syntax. The code I run in one platform without problems could not smoothly transform to the other platform. I still need to spend amount of time to recode it even though both programs have the same logic and same output.


Here is the code on X86_64 in GAS syntax.

 .text  
 .globl _start  
 start = 0  
 max = 30  
 _start:  
     mov  $start,%r15       /* starting value for the loop index*/  
 loop:  
     /*showing digit*/  
     mov   $'0',%r14  
     mov   $10,%r13  
     mov   $0,%rdx  
     mov   %r15,%rax  
     div   %r13  
     cmp   $0,%rax  
     je   singledigit  
     mov   %rax,%r13        /*store the second digit from right*/  
     add   %r14,%r13  
     mov   %r13,msg+5  
 singledigit:  
     mov   %rdx,%r12  
     add   %r14,%r12  
     mov   %r12,msg+6  
     /*showing loop in front of digit*/  
     mov   $len,%rdx  
     mov   $msg,%rsi  
     mov   $1,%rdi  
     mov   $1,%rax  
     syscall  
     inc   %r15              /* increment index*/  
     cmp   $max,%r15            /* see if we're done */  
     jne   loop              /* loop if we're not */  
     movq  $0,%rdi             /* exit status */  
     movq  $60,%rax            /* syscall sys_exit */  
     syscall  
 .data  
 msg:  .ascii   "loop:  '\n'"  
 .set len, . - msg  

And ARM assembly in aarch64:
 .text  
 .global _start  
 _start:  
     start = 0  
     max = 30  
     mov   x15,start        /*starting value for the loop index*/  
 loop:  
     mov   x28,0  
     mov   x27,10  
     adr   x12,msg         /*loading the message*/  
     udiv  x10,x15,x27       /*getting the quotient*/  
     msub  x9,x10,x27,x15     /*getting the reminder*/  
     cmp   x10,0          /*if quotient equals 0, then go to execute single digit*/  
     beq   singledigit  
     add   x14,x10,0x30      /*display quotient - second digit from right*/  
     str   x14,[x12,6]  
 singledigit:  
     add   x11,x9,0x30       /*display reminder - first digit from right*/  
     str   x11,[x12,7]  
     /*system call write*/  
     mov   x1,x12  
     mov   x2,len  
     mov   x0,1  
     mov   x8,64  
     svc   0  
     /*loop*/  
     add   x15,x15,1  
     cmp   x15,max  
     blt   loop  
     /*system exit*/  
     mov   x0,0  
     mov   x8,93  
     svc   0  
 .data  
 msg:  .ascii  "loop: ##\n\r"  
 len=  . - msg  

Through the practice in this lab, I got the basic idea how assembly works with memory and register, how to do the system call and how loop works.


In the end, I would like to ask if anyone has any good tutorials in assembly for beginners or suggestions, please comment it because I could not find one good for beginners. I highly appreciate.   

No comments:

Post a Comment