Skip to content

Instantly share code, notes, and snippets.

@vikage
Last active February 19, 2020 07:51
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vikage/10e5fc320f14c71f7457ae35efd8ec81 to your computer and use it in GitHub Desktop.
Save vikage/10e5fc320f14c71f7457ae35efd8ec81 to your computer and use it in GitHub Desktop.
[ASM] Input number and print sequence from 1 to number
.intel_syntax noprefix
.globl _main
.data
L.str.input:
.asciz "Input number: "
L.str.input_pattern:
.asciz "%d"
L.str.output_pattern:
.asciz "%d "
L.str.output:
.asciz "Your number: %d\n"
.text
print:
push rbp
mov rbp, rsp
sub rsp, 0x20
mov qword ptr [rbp - 0x8], rdi
mov qword ptr [rbp - 0x10], 1
# Start loop
LBB0_1:
# Check conditional i <= number
mov rax, qword ptr [rbp - 0x10]
cmp rax, qword ptr [rbp - 0x8]
jg LBB0_2
lea rdi, [rip + L.str.output_pattern]
mov rsi, qword ptr [rbp - 0x10]
call _printf
# Increase i
mov rax, qword ptr [rbp - 0x10]
add rax, 1
mov qword ptr [rbp - 0x10], rax
jmp LBB0_1 # Loop
LBB0_2: # Loop conditional fail
add rsp, 0x20
pop rbp
ret
exe:
push rbp
mov rbp, rsp
sub rsp, 0x10
# Print message input number
lea rdi, [rip + L.str.input]
call _printf
# Get number from keyboard
lea rdi, [rip + L.str.input_pattern]
lea rsi, qword ptr [rbp - 0x8]
call _scanf
# Print number user input
lea rdi, [rip + L.str.output]
mov rsi, qword ptr [rbp - 0x8]
call _printf
mov rdi, qword ptr [rbp - 0x8]
call print
add rsp, 0x10
pop rbp
ret
_main:
push rbp
call exe
mov rax, 0
pop rbp
ret
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment