Skip to content

Instantly share code, notes, and snippets.

@GabrielCastro
Last active August 29, 2015 14:08
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 GabrielCastro/a25bb2c51cb7336c5b43 to your computer and use it in GitHub Desktop.
Save GabrielCastro/a25bb2c51cb7336c5b43 to your computer and use it in GitHub Desktop.
Counting in x86 assembly
.text
.globl _start
_start:
# set return status to 0
mov $0,%rdi
# set up exit syscall
mov $60,%rax
syscall
.text
.globl _start
# where to start the loop
start = 0
max = 10
_start:
# put loop index in r15
mov $start,%r15
loop:
##
# empty loop body
##
# increment index
inc %r15
# compare
cmp $max,%r15
# go back to the top if it's not done
jne loop
# exit 0
mov $0,%rdi
mov $60,%rax
syscall
.text
.globl _start
# where to start the loop
start = 0
max = 10
_start:
# put loop index in r15
mov $start,%r15
loop:
##
# empty loop body
##
# increment index
inc %r15
# compare
cmp $max,%r15
# go back to the top if it's not done
jne loop
# exit 0
mov $0,%rdi
mov $60,%rax
syscall
.section .data
msg: .ascii "Loop: 0\n"
len = . - msg
dig = msg + 6
.text
.globl _start
start = 0 /* starting value for the loop index */
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 ... */
/* our code */
/* print "Loop: d\n"*/
# zero is a special case, we can't divide by zero
# but the string data already contains "Loop 0\n"
# so skip right to printing it
cmp $0,%r15
je print
# put n/10 in r14 && n%10 into r13
# integer division requires
# rdx = 0
# rax = dividend
# <any> = divisor, we'll use r12
mov $0,%rdx
mov %r15,%rax
mov $10,%r12
div %r12
# div gives us the result in rax and the modulus in rdx
# this is perfect for us because we actually need both of them
mov %rax,%r14
mov %rdx,%r13
# add "0" to both r14 and r13
# this converts single digit integer numbers to
# their acsii character values
add $'0',%r13
add $'0',%r14
# if the first digit is a '0' we'll replace it with a space
cmp $'0',%r14
jne not_zero
mov $' ',%r14
not_zero:
# move the address for the character into r13
mov $dig,%r12
# move the first character into the address in r13
mov %r14b,(%r12)
# move the address to the next character
inc %r12
# move the second character
mov %r13b,(%r12)
print:
mov $1,%rdi
mov $msg,%rsi
mov $len,%rdx
mov $1,%rax
syscall
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: 0\n"
len = . - msg
dig = msg + 6
.text
.globl _start
# where to start the loop
start = 0
max = 10
_start:
# put loop index in r15
mov $start,%r15
loop:
print:
# print the string
mov $1,%rdi
mov $msg,%rsi
mov $len,%rdx
mov $1,%rax
syscall
# increment index
inc %r15
# compare
cmp $max,%r15
# go back to the top if it's not done
jne loop
# exit 0
mov $0,%rdi
mov $60,%rax
syscall
.section .data
msg: .ascii "Loop: 0\n"
len = . - msg
dig = msg + 6
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment