forked from blau_araujo/pbn
diretórios 8 e 13 de exemplos
This commit is contained in:
parent
62338a8185
commit
6fbe5f3a62
13 changed files with 820 additions and 0 deletions
83
curso/exemplos/13/salve-read.asm
Normal file
83
curso/exemplos/13/salve-read.asm
Normal file
|
@ -0,0 +1,83 @@
|
|||
; ----------------------------------------------------------
|
||||
; Chamadas de sistema
|
||||
; ----------------------------------------------------------
|
||||
%define SYS_READ 0
|
||||
%define SYS_WRITE 1
|
||||
%define SYS_EXIT 60
|
||||
; ----------------------------------------------------------
|
||||
; Descritores de arquivos padrão
|
||||
; ----------------------------------------------------------
|
||||
%define STDIN_FD 0
|
||||
%define STDOUT_FD 1
|
||||
; ----------------------------------------------------------
|
||||
; Estados de término
|
||||
; ----------------------------------------------------------
|
||||
%define EXIT_SUCCESS 0
|
||||
; ----------------------------------------------------------
|
||||
; Constantes simbólicas
|
||||
; ----------------------------------------------------------
|
||||
%define BUF_SIZE 256
|
||||
; ----------------------------------------------------------
|
||||
section .rodata
|
||||
; ----------------------------------------------------------
|
||||
msg db `Salve, simpatia!\nQual é a sua graça?\n`
|
||||
msg_len equ $ - msg
|
||||
|
||||
resp db "Falaê, "
|
||||
resp_len equ $ - resp
|
||||
|
||||
tail db `!\n`
|
||||
; ----------------------------------------------------------
|
||||
section .bss
|
||||
; ----------------------------------------------------------
|
||||
buf resb BUF_SIZE ; Buffer de leitura
|
||||
count resd 1 ; retorno de read (int)
|
||||
; ----------------------------------------------------------
|
||||
section .text
|
||||
; ----------------------------------------------------------
|
||||
global _start
|
||||
_start:
|
||||
; Imprime a mensagem inicial
|
||||
mov rsi, msg
|
||||
mov rdx, msg_len
|
||||
call _print
|
||||
|
||||
; Aguarda os dados da entrada padrão
|
||||
mov rax, SYS_READ
|
||||
mov rdi, STDIN_FD
|
||||
mov rsi, buf
|
||||
mov rdx, BUF_SIZE
|
||||
syscall
|
||||
|
||||
; Salva retorno da chamada (bytes lidos)
|
||||
mov [count], eax
|
||||
|
||||
; Imprime prefixo da resposta
|
||||
mov rsi, resp
|
||||
mov rdx, resp_len
|
||||
call _print
|
||||
|
||||
; Imprime resposta
|
||||
mov rsi, buf
|
||||
mov rdx, [count] ; total de bytes lidos
|
||||
dec rdx ; desconta \n
|
||||
call _print
|
||||
|
||||
; Imprime final da resposta
|
||||
mov rsi, tail
|
||||
mov rdx, 2 ; !\n = 2 bytes
|
||||
call _print
|
||||
|
||||
; Termina o programa
|
||||
mov rax, SYS_EXIT
|
||||
mov rdi, EXIT_SUCCESS
|
||||
syscall
|
||||
; ----------------------------------------------------------
|
||||
; Sub-rotinas...
|
||||
; ----------------------------------------------------------
|
||||
_print:
|
||||
; ----------------------------------------------------------
|
||||
mov rax, SYS_WRITE
|
||||
mov rdi, STDOUT_FD
|
||||
syscall
|
||||
ret
|
Loading…
Add table
Add a link
Reference in a new issue