Created
September 28, 2019 05:53
-
-
Save P450/dd3d531c7b58ebc3c2189d0ddb926f57 to your computer and use it in GitHub Desktop.
This file contains hidden or 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) */ | |
zero = 48 /* ascii value for 0 */ | |
stdout = 1 | |
_start: | |
mov $start,%r15 /* loop index */ | |
loop: | |
mov $0,%rdx /* zero the rdx */ | |
mov %r15,%rax /* save index in rax */ | |
mov $10,%r13 /* move 10 (i.e. divisor) to r13 */ | |
div %r13 /* divide rax by 10, then save the quotient in rax and | |
the remainder into rdx (rdx must be zero before this!) */ | |
cmp $0,%rax /* compare quotient to 0*/ | |
je sloop /* if it's equal to 0, it's a single digit so jump to single digit loop */ | |
/* tens digit */ | |
add $zero,%rax /* increase rax by 48 (i.e. convert rax value to ascii value) */ | |
mov %rax,%r14 /* store the result in r14 */ | |
mov %r14b,msg+6 /* move 1 byte of r14 to msg+6 */ | |
/* ones digit */ | |
sloop: | |
add $zero,%rdx /* increase rdx by 48 (i.e. convert rdx value to ascii value)*/ | |
mov %rdx,%r14 /* store the result in r13 */ | |
mov %r14b,msg+7 /* move 1 byte of r13 to msg+7 (one more offset for formatting) */ | |
mov $len,%rdx /* message length */ | |
mov $msg,%rsi /* message location */ | |
mov $stdout,%rdi /* file descriptor */ | |
mov $1,%rax /* syscall sys_write */ | |
syscall | |
/* increment & check for exit */ | |
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 */ | |
syscall | |
.section .data | |
msg: .ascii "Loop: \n" | |
len = . - msg |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment