And Lesson 9: Stdin, goes into the X86_64 update pile.

This commit is contained in:
Elf M. Sternberg 2018-05-28 09:36:46 -07:00
parent 3ebb8fa0af
commit a8b5a39290
5 changed files with 95 additions and 0 deletions

1
.gitignore vendored
View File

@ -6,3 +6,4 @@ hello
strlen strlen
subroutines subroutines
includes includes
stdin

View File

@ -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 argv: argv.o ## Build Lesson 7 and 8: Line feeds and command line arguments
$(LD) -m $(LINK_32) -o $@ $< $(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) help: run-help ## Print this helpful message (default)
clean: clean:

45
x86/stdin.s Normal file
View File

@ -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

View File

@ -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 argv: argv.o ## Build Lesson 7 and 8: Line feeds and command line arguments
$(LD) -m $(LINK_64) -o $@ $< $(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) help: run-help ## Print this helpful message (default)
clean: clean:

43
x86_64/stdin.s Normal file
View File

@ -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