diff --git a/.gitignore b/.gitignore index 734a4fe..2d0e57f 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,4 @@ hello strlen subroutines includes +stdin diff --git a/x86/Makefile b/x86/Makefile index f38b5c6..e77b4c6 100644 --- a/x86/Makefile +++ b/x86/Makefile @@ -37,6 +37,9 @@ includes: includes.o ## Build Lesson 5: Separate strlen() and puts() into their argv: argv.o ## Build Lesson 7 and 8: Line feeds and command line arguments $(LD) -m $(LINK_32) -o $@ $< +stdin: stdin.o ## Build Lesson 9: Reading from stdin + $(LD) -m $(LINK_32) -o $@ $< + help: run-help ## Print this helpful message (default) clean: diff --git a/x86/stdin.s b/x86/stdin.s new file mode 100644 index 0000000..909d6d2 --- /dev/null +++ b/x86/stdin.s @@ -0,0 +1,45 @@ + ;; Lesson 9: User Input via STDIN. + +%include "functions.asm" +%define ILEN 255 ; Amount of space to reserve + + ;; sys/unistd_32.h +%define SYS_read 3 + + ;; unistd.h +%define STDIN 0 + + +section .data + msg1 db "Please enter your name: ", 0h + msg2 db "Hello, ", 0h + +section .bss + sinput resb ILEN ; Reserve 255 bytes of space. The space has no guarantees that I know of. + +section .text + global _start + +_start: + mov eax, msg1 + call putslf + + mov edx, ILEN + mov ecx, sinput + mov ebx, STDIN + mov eax, SYS_read + int 80h + + mov eax, msg2 + call puts + + mov eax, sinput + call puts ; By default, sinput keeps the LF terminator! + + call exit_program + + + + + + diff --git a/x86_64/Makefile b/x86_64/Makefile index c3b9a74..fdc0dc5 100644 --- a/x86_64/Makefile +++ b/x86_64/Makefile @@ -37,6 +37,9 @@ includes: includes.o ## Build Lesson 5: Separate strlen() and puts() into their argv: argv.o ## Build Lesson 7 and 8: Line feeds and command line arguments $(LD) -m $(LINK_64) -o $@ $< +stdin: stdin.o ## Build Lesson 9: Reading from stdin + $(LD) -m $(LINK_64) -o $@ $< + help: run-help ## Print this helpful message (default) clean: diff --git a/x86_64/stdin.s b/x86_64/stdin.s new file mode 100644 index 0000000..b9d18cb --- /dev/null +++ b/x86_64/stdin.s @@ -0,0 +1,43 @@ + ;; Lesson 9: User Input via STDIN. + +%include "functions.asm" +%define ILEN 255 ; Amount of space to reserve + + ;; sys/unistd_64.h +%define SYS_read 0 + + ;; unistd.h +%define STDIN 0 + + +section .data + msg1 db "Please enter your name: ", 0h + msg2 db "Hello, ", 0h + +section .bss + sinput resb ILEN ; Reserve 255 bytes of space. The space has no guarantees that I know of. + +section .text + global _start + +_start: + ;; Again, the challenge with x86_64 is knowing what register to us. + ;; In this case, RSI - Source Indexing. + mov rsi, msg1 + call puts + + mov rax, SYS_read ; General purpose register + mov rdi, STDIN ; Destination Index + mov rsi, sinput ; Source register + mov rdx, ILEN ; Common counter + + ;; And remembering to use 'syscall' instead of interrupt-80 + syscall + + mov rsi, msg2 + call puts + + mov rsi, sinput + call puts + + call exit_program