Created
May 10, 2022 02:54
-
-
Save Colouratura/8c9c1f73eaeb40dcc018719ef4469b51 to your computer and use it in GitHub Desktop.
Hello with input in NASM assembly for x86-64 linux with the common ABI
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
; Build: | |
; ~$ nasm -felf64 hello-input.s -o hello-input.o | |
; ~$ ld hello-input.o | |
; ~$ ./a.out | |
global _start | |
section .data | |
null: equ 0x0 | |
stdoutf: equ null | |
stdinf: equ 0x1 | |
newline: equ 0xA | |
namelen: equ 0xF | |
prompt: db 'What is your name: ', null | |
promptlen: equ $-prompt | |
greet: db 'Hello, ', null | |
greetlen: equ $-greet | |
section .bss | |
name: resb namelen | |
section .text | |
_start: | |
; write prompt to stdout | |
mov rax, 0x1 ; rax = write (0x1) | |
mov rdi, stdoutf ; rdi = stdout (0x0) | |
lea rsi, prompt ; rsi = *prompt | |
mov rdx, promptlen ; rdx = promptlen | |
syscall ; write(stdout, prompt, promptlen) | |
; read into name | |
mov rax, 0x0 ; rax = read (0x0) | |
mov rdi, stdinf ; rdi = stdin (0x1) | |
lea rsi, name ; rsi = *name | |
mov rdx, namelen ; rdx = namelen (0xF) | |
syscall ; read(stdin, name, namelen) | |
; write greeting | |
mov rax, 0x1 ; rax = write (0x1) | |
mov rdi, stdoutf ; rdi = stdout (0x0) | |
lea rsi, greet ; rsi = *greet | |
mov rdx, greetlen ; rdx = greetlen | |
syscall ; write(stdout, greet, greetlen) | |
; print name | |
mov rax, 0x1 ; rax = write (0x1) | |
mov rdi, stdoutf ; rdi = stdout (0x0) | |
lea rsi, name ; rsi = *name | |
mov rdx, namelen ; rdx = namelen (0xF) | |
syscall ; write(stdout, name, namelen) | |
; exit | |
mov rax, 0x3C ; rax = exit (0x3C) | |
mov rdi, 0x0 ; rdi = 0x0 | |
syscall ; exit(0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment