Last active
January 30, 2017 02:46
-
-
Save blotta/ae32fad93f37e5050030ffe4fe6c4687 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.text | |
.globl _start | |
start = 0 /* starting value for the loop index; note that this is a symbol (constant), not a variable */ | |
max = 31 /* loop exits when the index hits this number (loop condition is i<max) */ | |
_start: | |
mov $start,%r15 /* loop index */ | |
loop: | |
/* ... body of the loop ... do something useful here ... */ | |
/* Printing .msg */ | |
movq $len,%rdx /*Length of data in address pointed by msg*/ | |
movq $msg,%rsi /*Message data on msg*/ | |
movq $1,%rdi /*1 for stdout*/ | |
movq $1,%rax /*sys_write instruction*/ | |
syscall | |
/*dividing counter */ | |
movq $0,%rdx /*rdx must be zero before using div*/ | |
movq %r15,%rax /*moving counter to rax*/ | |
movq $10,%r12 | |
div %r12 /* counter/10 -> int in rax; remainder in rdx*/ | |
movq %rax,%r13 /* save int in r13 */ | |
movq %rdx,%r14 /* save remainder in r14 */ | |
/*leading digit*/ | |
movq $len2,%rdx /*Length of 1*/ | |
movq $digit,%rsi /*digit msg(space)*/ | |
cmp $0,%r13 /* 0 - r13 */ | |
je lsd /* jump to lsd if last comparison is zero */ | |
movb %r13b,(%rsi)/* move quotient to rsi */ | |
add $48,(%rsi) /* ascii conversion */ | |
movq $1,%rdi /*stdout */ | |
movq $1,%rax /*sys_write */ | |
syscall | |
lsd: | |
/*following digit*/ | |
movq $len2,%rdx /*Length*/ | |
movq $digit,%rsi /*Message data on digit (blank space)*/ | |
movb %r14b,(%rsi)/*Moves loop counter to address pointed by rsi (digit)*/ | |
add $48,(%rsi) /*Adds 48d. The first loop value is zero*/ | |
movq $1,%rdi /*stdout*/ | |
movq $1,%rax /*sys_write*/ | |
syscall | |
/* Printing newline character*/ | |
movq $len1,%rdx | |
movq $nl,%rsi | |
movq $1,%rdi | |
movq $1,%rax | |
syscall | |
/* loop condition */ | |
inc %r15 /* increment index */ | |
cmp $max,%r15 /* max - r15 */ | |
jl loop /* if negative flag = 1, continue. Else, call loop */ | |
/* the 2 expression above mean 'if (max > r15){loop()};*/ | |
/*exit*/ | |
mov $0,%rdi /* exit status */ | |
mov $60,%rax /* syscall sys_exit */ | |
syscall | |
.section .data | |
msg: .ascii "Loop: " | |
len = . - msg | |
nl: .ascii "\n" | |
len1 = . - nl | |
digit: .ascii " " | |
len2 = . - digit |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment