Lab 4 – Assembler

This lab is all about learning assembler, and the differences between x86_64 and aarch64 assembler.

To begin we need to make this output in both architectures:

Loop: 0
Loop: 1
Loop: 2
Loop: 3
Loop: 4
Loop: 5
Loop: 6
Loop: 7
Loop: 8
Loop: 9

So, here is the x86 code:

.globl _start

start = 0 /* starting value for the loop index; note that
this is a symbol (constant), not a variable */
max = 10 /* loop exits when the index hits this number (l
oop condition is i < max) */
offset = 48

mov $start,%r15 /* loop index */

/* ... body of the loop ... do something useful here ... */

movq $len,%rdx /* message length */

movb $offset, %al /* Put offset in register a low */
add %r15,%rax /* add counter to register a */
mov %al,msg + 6 /* put a low into the memory of msg + 6 */

movq $msg,%rsi
movq $1,%rdi /* file descriptor stdout */
movq $1,%rax /* syscall sys_write */

inc %r15 /* increment index */
cmp $max,%r15 /* see if we're done */
jne loop /* loop if we're not */

mov $0,%rdi /* exit status */
mov $60,%rax /* syscall sys_exit */

.section .data

msg: .ascii "Loop: \n"
len = . - msg

So to get the output we have an offset value (48) that lets us convert the counter to the ascii number character. We put out offset number into a register, then add our counter to that. Then we have to stuff that into the correct place to have it be in out message. In this case we will put it in msg + 6.

Here is the aarch64 assembler for the same output.

.globl _start

start = 0
max = 10
offset = 48

mov x19,start

add x20,x19,offset /*number counter stuff*/
adr x21, msg /*put msg in a register*/
strb w20, [x21, 6] /*store our add result into msg*/

mov x0, 1 /* file descriptor: 1 is stdout */
adr x1, msg /*message location (memory address) */
mov x2, len /* message length (bytes) */

mov x8, 64 /* write is syscall #64 */
svc 0 /*invoke syscall */

add x19,x19,1 /*LOOP CHECKING STUFF*/
cmp x19,max _loop

mov x0, 0 /* status -> 0*/
mov x8, 93 /* exit is syscall #93 */
svc 0 /* invoke syscall*/

msg: .ascii "Loop: \n"
len= . - msg

This code is very similar to the x86_64 code. The three argument add instruction allows us to do the mov and add of offset in one step, which makes this code significantly nicer to write. However we need to load msg into a register, which we do not have to do on x86_64.

Overall from these programs, aarch64 appears to me to be easier to work with than x86_64. The instructions also are more readable than the x86_64 instructions.