[英]Why doesn't this assembly program work correct?
我正在嘗試制作一個簡單的 x86 匯編代碼,它從用戶那里獲取輸入,然后將其與內存中的硬編碼密碼進行匹配,並相應地向用戶打印一條消息(正確/不正確)。
這是我的代碼:
%macro write_string 2
mov eax, 4
mov ebx, 1
mov ecx, %1
mov edx, %2
int 80h
%endmacro
%macro read_string 2
mov eax, 3
mov ebx, 0
mov ecx, %1
mov edx, %2
int 80h
%endmacro
section .text
global _start
_start:
write_string msg, lenMsg
read_string string, 32
mov [readBytes], eax
mov ecx, 0
l1:
mov eax, string
add eax, ecx
mov byte al, [eax]
mov eax, pwd
add eax, ecx
mov byte bl, [eax]
cmp bl, al
jne incorrect
inc ecx
cmp ecx, [readBytes]
jne l1
correct:
write_string txt1, txt1Len
jmp exit
incorrect:
write_string txt2, txt2Len
exit:
write_string lineBreak, 1
mov eax,1
int 0x80
section .bss
readBytes: resb 1
section .data
msg db 'Password: '
lenMsg equ $ - msg
string times 100 db 0
pwd db 'secretPass1'
txt1 db 'Correct', 0xa
txt1Len equ $ - txt1
txt2 db 'Incorrect', 0xa
txt2Len equ $ - txt2
lineBreak db 0xa
程序可以編譯,但是當我運行它並輸入一個輸入時,它總是打印“不正確”的消息,即使我使用了正確的密碼。
你能告訴我我做錯了什么嗎?
我還有一個問題,它們之間有什么區別:
section .bss
string: resb 100
和:
section .data
string times 100 db 0
用於分配未初始化的數據?
謝謝
mov byte al, [eax] mov eax, pwd
正如ecm 告訴您的那樣,將地址加載到pwd 會覆蓋已加載到AL
寄存器中的字符。
您可以通過使用不同的寄存器來遍歷pwd來解決這個問題,或者您可以通過使用不同的尋址模式來糾正它並簡化代碼:
mov [readBytes], eax
xor ecx, ecx
Verify:
mov dl, [string + ecx]
mov bl, [pwd + ecx]
cmp dl, bl
jne incorrect
inc ecx
cmp ecx, eax ; No need to compare with a memory based variable
jb Verify
還有其他問題:
在 .bss 中,您為readBytes變量保留了1 個字節,該變量實際上是一個雙字,因此需要4 個字節的存儲空間。 因為您只在 .bss 中使用了那個變量,所以您現在不會注意到錯誤,而是等到您(必須)編寫具有更多變量的程序!
您的密碼驗證循環會遍歷所有輸入的字符,而忽略這樣一個事實:如果輸入比存儲的密碼更長或更短(包括空) ,則比較這些字符將變得徒勞無功。
mov [readBytes], eax cmp eax, 11 ; Length of the hardcode password 'secretPass1' jne incorrect xor ecx, ecx Verify:
resb 100
(.bss) 和times 100 db 0
(.data) 之間的差異。
匯編程序執行times 100 db 0
。 它在您的可執行文件中實際放置了這么多零字節。 您可以保證內存將包含 100 個零字節。
resb 100
在程序啟動時由操作系統“執行”。 很多時候這個內存也將包含零字節,但這個想法是你不應該指望這是真的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.