read-print file
This commit is contained in:
parent
7b413d540c
commit
703a8ba968
|
|
@ -10,6 +10,8 @@ section .data
|
||||||
file_error_msg_len equ $ - file_error_msg
|
file_error_msg_len equ $ - file_error_msg
|
||||||
error_msg db "Error: "
|
error_msg db "Error: "
|
||||||
error_msg_len equ $ - error_msg
|
error_msg_len equ $ - error_msg
|
||||||
|
buffer_size equ 1024
|
||||||
|
buffer times buffer_size db 0
|
||||||
|
|
||||||
section .text
|
section .text
|
||||||
global _start
|
global _start
|
||||||
|
|
@ -30,14 +32,37 @@ _start:
|
||||||
; open file for reading
|
; open file for reading
|
||||||
mov rdx, rax ; filename pointer
|
mov rdx, rax ; filename pointer
|
||||||
call fopen_read
|
call fopen_read
|
||||||
mov rax, 42
|
|
||||||
; exit with success
|
.read_loop:
|
||||||
mov rdx, 0
|
mov r9, rax ; file descriptor
|
||||||
call exit
|
mov rax, 0 ; syscall: read
|
||||||
|
mov rdi, r9 ; fd
|
||||||
|
lea rsi, [buffer] ; buffer
|
||||||
|
mov rdx, buffer_size ; size
|
||||||
|
syscall
|
||||||
|
cmp rax, 0 ; check for EOF
|
||||||
|
jle .exit ; if rax <= 0, exit loop
|
||||||
|
mov rcx, rax ; number of bytes read
|
||||||
|
|
||||||
|
; write to stdout for now
|
||||||
|
mov rax, 1 ; syscall: write
|
||||||
|
mov rdi, 1 ; fd: stdout
|
||||||
|
lea rsi, [buffer] ; buffer
|
||||||
|
mov rdx, rcx ; len: bytes read
|
||||||
|
syscall
|
||||||
|
jmp .read_loop
|
||||||
|
|
||||||
.no_filename:
|
.no_filename:
|
||||||
call panic
|
call panic
|
||||||
|
.exit:
|
||||||
|
call exit
|
||||||
|
|
||||||
|
|
||||||
|
;; ==============================
|
||||||
|
;; Helper functions
|
||||||
|
;; ==============================
|
||||||
|
|
||||||
|
;; Abort the program with a default panic message
|
||||||
panic:
|
panic:
|
||||||
mov rdx, panic_msg
|
mov rdx, panic_msg
|
||||||
mov rcx, panic_msg_len
|
mov rcx, panic_msg_len
|
||||||
|
|
@ -54,10 +79,6 @@ exit:
|
||||||
mov rdi, rdx
|
mov rdi, rdx
|
||||||
syscall
|
syscall
|
||||||
|
|
||||||
;; ==============================
|
|
||||||
;; Helper functions
|
|
||||||
;; ==============================
|
|
||||||
|
|
||||||
;; Writes a string to stderr:
|
;; Writes a string to stderr:
|
||||||
;; rdx: pointer to string
|
;; rdx: pointer to string
|
||||||
;; rcx: length of string
|
;; rcx: length of string
|
||||||
|
|
@ -147,4 +168,4 @@ fopen_read:
|
||||||
|
|
||||||
pop rsi
|
pop rsi
|
||||||
add rsp, rsi ; dealloc
|
add rsp, rsi ; dealloc
|
||||||
ret
|
call panic
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue