Skip to content

Instantly share code, notes, and snippets.

@ruanchaves
Created April 20, 2018 11:58
Show Gist options
  • Save ruanchaves/b0caf393edf0637add2ef7f0f543b55b to your computer and use it in GitHub Desktop.
Save ruanchaves/b0caf393edf0637add2ef7f0f543b55b to your computer and use it in GitHub Desktop.
Assembly program to turn 123 into 321 and print it.
.data
Sf: .string "%d\n"
.text
.globl main
main:
# start main
pushq %rbp
movq %rsp, %rbp
subq $16, %rsp
movq %rbx, -8(%rbp)
movq %r12, -16(%rbp)
movl $123, %esi # int n
movl $0, %edi # int reverse
movl $10, %ebx # divisor
L1: # while n != 0
cmpl $0, %esi
je L2
# reverse = reverse * 10
imul $10, %edi
# reverse = reverse + n % 10
movl $0, %edx
movl %edi, %eax
idivl %ebx
addl %edx, %edi
# n = n / 10
movl %esi, %eax
movl $0, %edx
idivl %ebx
movl %eax, %esi
jmp L1
L2: # end while
movl %edi, %eax
# print value
movq $Sf, %rdi
movl %eax, %esi
movl $0, %eax
call printf
# end main
movq $0, %rax
movq -8(%rbp), %rbx
movq -16(%rbp), %r12
leave
ret
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment