Skip to content

Commit

Permalink
Properly implement read
Browse files Browse the repository at this point in the history
  • Loading branch information
NikitaIvanovV committed Apr 24, 2022
1 parent 963efc9 commit 5f56e94
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 8 deletions.
1 change: 1 addition & 0 deletions snake.asm
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,7 @@ run:

.loop:
mov rax, input
mov rdx, 1
call poll

call handle_key
Expand Down
33 changes: 25 additions & 8 deletions syscall.asm
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,14 @@ section .text

global poll

; rax: address to save input to
; rax: buffer
; rdx; count
poll:
push rdi
push rsi

push rax ; save input addr
push rax ; save buffer
push rdx ; save count

; poll event
mov rdi, poll_fd ; pointer to struct
Expand All @@ -100,13 +102,13 @@ poll:
SYS POLL

mov rsi, rax
pop rax ; restore input addr
pop rdx ; restore count
pop rax ; restore buffer

test rsi, rsi ; test if no event
jz .no_event

; read input
mov rdx, 1
call read

jmp .exit
Expand Down Expand Up @@ -146,11 +148,26 @@ read:

mov rsi, rax
mov rdi, STDIN
SYS READ

pop rsi
pop rdi
ret
.loop:
SYS READ

; exit if EOF
test rax, rax
je .exit

; exit if read as many bytes as requested
cmp rax, rdx
je .exit

; read less then requested, repeat syscall
sub rdx, rax
jmp .loop

.exit:
pop rsi
pop rdi
ret

section .data

Expand Down

0 comments on commit 5f56e94

Please sign in to comment.