我正在嘗試組裝 x86,但出現zsh: segmentation fault ./test錯誤。我正在嘗試自己制作一些基本庫以供以后使用。它分為三個檔案 - string.asm 用于字串操作,stdio.asm 用于標準 io 操作,libtest.asm 用于庫測驗。我計劃添加更多功能,但我想先測驗這些功能。
;;;;;;;;;;;;;;;;;
;;; stdio.asm ;;;
;;;;;;;;;;;;;;;;;
global print ; void print(string* text)
extern strlen ; int32 strlen(string* string)
section .data
stdin dd 0
stdout dd 1
stderr dd 2
newline db 0x0a, 0x00
section .text
print:
push ebp
mov ebp, esp
push eax
push ebx
push ecx
push edx
; get parameters
mov ecx, dword [ebp 8]
; call strlen
push ecx
call strlen
add esp, 4
; moving length
mov ecx, eax
; moving syscall num and out desc
mov eax, 4
mov ebx, [stdout]
; syscall
int 0x80
pop edx
pop ecx
pop ebx
pop eax
mov esp, ebp
pop ebp
ret
;;;;;;;;;;;;;;;;;
;; string.asm ;;;
;;;;;;;;;;;;;;;;;
global strlen ; int32 strlen(string* str)
section .text
strlen:
push ebp
mov ebp, esp
push ebx
mov ebx, dword [ebp 8]
mov eax, 0
loop1:
cmp [ebx eax], byte 0x00
inc eax
jne loop1
dec eax
pop ebx
mov esp, ebp
pop ebp
ret
;;;;;;;;;;;;;;;;;
;; libtest.asm ;;
;;;;;;;;;;;;;;;;;
global _start
extern print
section .data
msg db 'Hello, world!', 0x0a, 0x00
section .text
_start:
push msg
call print
add esp, 4
mov eax, 1
mov ebx, 0
int 0x80
執行:
$ nasm -f elf32 stdio.asm
$ nasm -f elf32 string.asm
$ nasm -f elf32 libtest.asm
$ ld -m elf_i386 -o test stdio.o string.o libtest.o
$ ./test
我不知道錯誤可能出在哪里,所以我希望你們能提供任何幫助。
謝謝!
uj5u.com熱心網友回復:
兩個錯誤:
; moving length
mov ecx, eax
; moving syscall num and out desc
mov eax, 4
mov ebx, [stdout]
; syscall
int 0x80
參考Linux 系統呼叫約定,write系統呼叫需要緩沖區指標 inecx和長度 in edx。你有長度,ecx緩沖區指標根本不存在。做了:
mov edx, eax
mov ecx, dword [ebp 8]
mov eax, 4
mov ebx, [stdout]
int 0x80
接下來看看:
cmp [ebx eax], byte 0x00
inc eax
jne loop1
該inc指令根據其輸出設定零標志。因此,您jne不會根據 的結果進行分支cmp,而是根據是否eax遞增為零(即環繞)。所以你的回圈會迭代太多次。
jne需要緊跟在 之后,cmp中間沒有其他標志修改指令。有幾種方法可以重寫。一種是:
mov eax, -1
loop1:
inc eax
cmp byte [ebx eax], 0x00
jne loop1
請注意,這消除了dec eax最后額外的需要。
修復這些后,該程式對我有用。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/463858.html
