Lab 6: Làm Việc Với Tập Tin: Thực hành CTMT&HN-2019 Khoa Cntt - Đhspkt Tp.Hcm Trang - 1
Lab 6: Làm Việc Với Tập Tin: Thực hành CTMT&HN-2019 Khoa Cntt - Đhspkt Tp.Hcm Trang - 1
section .data
msg: db "syscall1_64.asm running",10 ; the string to print,
10=crlf
len: equ $-msg ; "$" means here, len is a value, not an address
msg2: db "syscall1_64.asm finished",10
len2: equ $-msg2
msg3: db "syscall1_64.asm opened",10
len3: equ $-msg3
msg4: db "syscall1_64.asm read",10
len4: equ $-msg4
msg5: db "syscall1_64.asm open fail",10
len5: equ $-msg5
msg6: db "syscall1_64.asm another open fail",10
len6: equ $-msg6
msg7: db "syscall1_64.asm read fail",10
len7: equ $-msg7
extern open
global main
section .text
main:
push rbp ; set up stack frame
; header msg
mov rdx,len ; arg3, length of string to print
mov rcx,msg ; arg2, pointer to string
mov rbx,1 ; arg1, where to write, screen
mov rax,4 ; write command to int 80 hex
int 0x80 ; interrupt 80 hex, call kernel
read:
; file opened msg3
mov rdx,len3 ; arg3, length of string to print
mov rcx,msg3 ; arg2, pointer to string
mov rbx,1 ; arg1, where to write, screen
mov rax,4 ; write command to int 80 hex
int 0x80 ; interrupt 80 hex, call kernel
doread:
mov rdx,8192 ; max to read
mov rcx,line ; buffer
mov rbx,[fd] ; fd
mov rax,3 ; read command to int 80 hex
int 0x80 ; interrupt 80 hex, call kernel
mov [lenbuf],rax ; number of characters read
cmp rax,0 ; test for fail
jg readok ; some read
write:
mov rdx,[lenbuf] ; length of string to print
mov rcx,line ; pointer to string
mov rbx,1 ; where to write, screen
mov rax,4 ; write command to int 80 hex
int 0x80 ; interrupt 80 hex, call kernel
fail:
; finished msg2
mov rdx,len2 ; arg3, length of string to print
mov rcx,msg2 ; arg2, pointer to string
mov rbx,1 ; arg1, where to write, screen
mov rax,4 ; write command to int 80 hex
int 0x80 ; interrupt 80 hex, call kernel
%assign SYS_EXIT 1
%assign READ 3
%assign WRITE 4
%assign STDOUT 1
%assign STDIN 0
%assign ENDL 0x0a
;; -------------------------
;; data segment
;; -------------------------
section .bss
inmsg: resb 255
inlen: resd 1
;; -------------------------
;; code area
;; -------------------------
section .text
global _start
_start:
mov eax,WRITE ;4
mov ebx,STDOUT ;1
lea ecx,[msg] ;address of source
mov edx,MSGLEN ; length (num of characters)
int 0x80
mov edi, 1
mov byte [inmsg], 0 ;init with end of string for the loop
mov ecx, -1
loopit: inc ecx
mov al, [inmsg + ecx]
cmp al, [strEnd]
JE endlp
cmp al, 0x41
JB loopit
cmp al, 0x5A
JAE loopit
Thực hành CTMT&HN-2019 Khoa CNTT – ĐHSPKT TP.HCM Trang - 5
add al, 0x20
mov [inmsg + ecx], al
jmp loopit
endlp: mov eax, WRITE ; write the input string back out
mov ebx, STDOUT
lea ecx, [inmsg]
mov edx, edi ; edi now contains the string length
int 0x80
;; exit()
mov eax,SYS_EXIT
mov ebx,0
int 0x80 ; final system call
6.3.