-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathread-from-console.asm
More file actions
53 lines (44 loc) · 1.09 KB
/
read-from-console.asm
File metadata and controls
53 lines (44 loc) · 1.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
; Read all input from STDIN and represent it in STDOUT until empty input provided
%define SYS_read 0x2000003
%define SYS_write 0x2000004
%define SYS_exit 0x2000001
%define STDIN 0
%define STDOUT 1
global _main ; Entry point
section .bss
inputBuffer resb 16
inputBufferSize equ $-inputBuffer
section .text
_main:
call read_write_loop
call exit
ret
; To properly handle read process it's required to utilize all peding data
; So read all chunks until empty input found
read_write_loop:
.while_loop_body:
call read_input ; RAX contains SYS_read return value,
cmp rax, 1 ; in case of 1, then no input provided (\0 only)
je .while_loop_end ; and we can finish
call write_input
jmp .while_loop_body
.while_loop_end:
ret
read_input:
mov rax, SYS_read
mov rdi, STDIN
mov rsi, inputBuffer
mov rdx, inputBufferSize
syscall
ret
write_input:
mov rax, SYS_write
mov rdi, STDOUT
mov rsi, inputBuffer
mov rdx, inputBufferSize
syscall
ret
exit:
mov rax, SYS_exit
mov rdi, 0
syscall