Skip to content

Instantly share code, notes, and snippets.

@Protoster
Created November 23, 2015 03:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Protoster/e50ba25046db53edb146 to your computer and use it in GitHub Desktop.
Save Protoster/e50ba25046db53edb146 to your computer and use it in GitHub Desktop.
SPO600 Lab 4 assembly code
.text
.globl _start
_start:
max=31
mov x9, 0 /* iteration counter */
mov x14, 10 /* division doesnt support immediate values */
adr x11, msg /* address of text string */
loop:
cmp x9, x14
b.lt second /* dont show second digit under 10 */
udiv x12, x9, x14
add x13, x12, '0'
strb w13, [x11, 6]
second:
msub x10, x14, x12, x9 /* mod 10 the counter to get the first digit */
add x10, x10, '0'
strb w10, [x11, 7]
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 x9, x9, 1
cmp x9, max
b.ne loop
mov x0, 0 /* status -> 0 */
mov x8, 93 /* exit is syscall #93 */
svc 0 /* invoke syscall */
.data
msg: .ascii "Loop: \0 \n"
len= . - msg
.text
.globl _start
_start:
max=31
mov $0, %r9 /* iteration counter */
mov $10, %r14 /* division doesnt support immediate values */
mov $msg, %r10
loop:
mov $0, %rdx
mov %r9, %rax
div %r14
/*cmp %r9, %r14
jl second /* dont show second digit under 10 */
add $'0', %rax
movb %al, 6(%r10)
second:
add $'0', %rdx
movb %dl, 7(%r10)
movq $len, %rdx /* message length */
movq $msg, %rsi /* message location */
movq $1, %rdi /* file descriptor stdout */
movq $1, %rax /* syscall sys_write */
syscall
inc %r9
cmp $max, %r9
jne loop
movq $0, %rdi /* exit status */
movq $60, %rax /* syscall sys_exit */
syscall
.section .data
msg: .ascii "Loop: \0 \n"
len= . - msg
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment