52 lines
1.2 KiB
NASM
52 lines
1.2 KiB
NASM
global _start
|
|
|
|
section .data
|
|
str: db "Please give me a string: " ; \n = 0xA
|
|
|
|
strSize: equ $ - str
|
|
; $ = current assembler address.
|
|
; Since str is all that is above it must be the size
|
|
|
|
stdin: equ 0 ; POSIX
|
|
stdout: equ 1 ; POSIX
|
|
|
|
; Uninitialized space goes here, pretty much your heap of uninitialized bytes
|
|
; for all kinds of purposes
|
|
section .bss
|
|
strBuffer: resb 64 ; reserve 64 bytes
|
|
strBufferSize: equ 64 ; same as above
|
|
|
|
; This is where the code goes. Text. Makes sense, right?
|
|
section .text
|
|
_start:
|
|
; x64 has the syscall function, in contrast to int in x86 and ARM
|
|
|
|
; Write to stdout
|
|
mov rax, 1
|
|
mov rdi, stdout ; fd to write to
|
|
mov rsi, str ; pointer
|
|
mov rdx, strSize ; size
|
|
syscall ; all arguments are specified above! (x86 = int 0x80)
|
|
|
|
; Read from stdin
|
|
xor rax, rax ; 0
|
|
mov rdi, stdin ; fd to read from
|
|
mov rsi, strBuffer ; where to store it
|
|
mov rdx, strBufferSize ; maximum number of bytes
|
|
syscall
|
|
|
|
; syscall return codes are always stored in register A
|
|
; The number of bytes read in was returned through this
|
|
mov rdx, rax
|
|
|
|
mov rax, 1
|
|
mov rdi, stdout
|
|
mov rsi, strBuffer
|
|
syscall
|
|
|
|
|
|
; Exit routine
|
|
mov rax, 60 ; 60 is the exit routine
|
|
xor rdi, rdi ; our return code (0)
|
|
syscall
|