当谈到 FASM 时,我是一个新手,而且对 ASM 来说一般来说还是很新的,但我正在尝试比较存储在“变量”中的两个字符串:user_input
和exit_cmd
:
目前,它组装得很好,但是当我在提示中输入任何内容时它会崩溃。是的,我的代码很乱,我试图完成的任务似乎超出了我所知道的 ASM 水平,但我已经用其他语言完成了它,所以我正在用 ASM 进行尝试。
如果字符串匹配,您可以看到我正在使用宏CompareStrings
(来源未知;它不是我的)设置EAX
为 1,但是当我EAX
使用 1CMP
与JE
标签进行比较时,它拒绝工作。有什么帮助吗?
这是错误的代码:
format PE console
;win32a.inc, win32w.inc, win64a.inc and win64w.inc
include 'win32w.inc'
entry main
; Define macros for things like printing strings ('print') and pausing (well, waiting for the user to press enter ('pause'))
macro print str {
push str
call [printf]
push 0
}
macro pause {
invoke system,'pause>nul'
}
macro getinput prompt {
print prompt
invoke gets,user_input
}
macro CompareStrings str1, str2
{
lea edi, [str2] ; edi -> address of string2
dec edi ; edi = edi - 1
.lab1: ; loop through all chars and compare each of them
inc edi ; ds:di --> next character in string2
lodsb ; al = next char from string1. loadsb increments si automatically
cmp [edi], al ; compare characters
jne .notfound ; jump out of the loop if they're unequal
cmp al, 0 ; chars are equal, but make sure we compared the entire string
jne .lab1 ; if not, continue the loop
mov eax, 1 ; else: strings are equal -> eax = 1
ret ; return; result: strings are equal
.notfound: ; chars are not equal
mov eax, 0 ; unequal -> eax = 0
ret ; return; result: strings are not equal
}
section '.text' code readable executable
main:
print header_msg
jmp input_loop
input_loop:
getinput cmd_prompt
print newline
CompareStrings user_input,cmd_exit
cmp eax, 1
je exit_cmd
jne input_loop
exit_cmd:
print header_msg
ret
section '.data' data readable writable
header_msg db "Basic Programming Language in ASM | Version 0.0.1",13,10,0
cmd_prompt db "> ",0
newline db "",13,10,0
user_input db "",0dh,0ah,0
chrarr db '%s',0dh,0ah,0
cmd_exit db 'exit',0
section '.idata' import data readable
library msvcrt,"msvcrt.dll"
import msvcrt,\
puts,"puts",\
getchar,"getchar",\
exit,"exit",\
printf,"printf",\
scanf,"scanf",\
system,"system",\
gets,"gets"