Created
December 26, 2018 18:17
-
-
Save b4284/4d760319e30fa2365a88b9cdb952a7bf to your computer and use it in GitHub Desktop.
A practice of reading words from standard input using NASM
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
all: readword | |
readword: readword.o | |
ld -o $@ $^ | |
readword.o: readword.asm | |
nasm -felf64 -o $@ $^ |
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
global _start | |
section .text | |
_start: | |
print_prompt: | |
mov rax, 1 ; syscall write | |
mov rdi, 1 ; stdout | |
mov rsi, prompt ; prompt string | |
mov rdx, 2 ; 2 bytes | |
syscall | |
mov r8, 0 ; length of buffer | |
mov rsi, buffer ; initialize buffer location | |
mov r10, 0 ; reset EOF flag | |
read_char: | |
mov rax, 0 ; syscall read | |
mov rdi, 0 ; stdin | |
mov rdx, 1 ; 1 byte | |
syscall | |
cmp rax, 0 ; is EOF? | |
jne non_eof ; jump to process char if not EOF (read=0) | |
mov r10, 1 ; flag EOF | |
jmp print_word ; print_word | |
non_eof: | |
inc r8 ; increment buffer length | |
inc rsi ; increment buffer location | |
mov r9, 0 ; clear r9 | |
mov r9b, byte [rsi-1] ; move previous char into r9 | |
cmp r9, 10 ; is it NEWLINE? | |
je print_word ; print the word if it is (including the NEWLINE) | |
cmp r8, buffer_size ; compare current length with buffer_size | |
je print_word ; print the word if it's equal to buffer_size | |
jmp read_char ; read next character | |
print_word: | |
mov rax, 1 ; syscall write | |
mov rdi, 1 ; stdout | |
mov rsi, buffer ; buffer | |
mov rdx, r8 ; length of word | |
syscall | |
cmp r10, 1 ; is EOF? | |
je exit | |
jmp print_prompt | |
exit: | |
mov rax, 60 ; system call for exit | |
xor rdi, rdi ; exit code 0 | |
syscall ; invoke operating system to exit | |
section .bss | |
buffer_size equ 64 | |
buffer: | |
resb buffer_size ; reserve a 64-byte buffer | |
section .data | |
prompt: | |
dw "> " |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment