【发布时间】:2015-11-20 17:56:47
【问题描述】:
在这段代码中,我试图让编译器识别字符串中的第二个字符。比如我输入haris;然后我希望能够迭代到字符串“haris”中的“a”。我使用 lea 获取指向寄存器存储“haris”的指针。然后我增加了寄存器eax,它用作指针,最初指向“h”,所以它应该指向“a”——对吗?但是,我的代码似乎没有按预期工作,因为如果它工作,跳转到 _works 应该工作。
extern _printf
extern _scanf
extern _printf
extern _system
extern _strcmp
segment .data
szPAUSE db'PAUSE', 0
szStrIn db '%s',0
szName db 0,0
szNworks db 'Doesnt work', 10,0
szworks db 'Does work', 10,0
segment .code
global _main
_main:
enter 0,0
;get name. I input "haris" for test
push szName
push szStrIn
call _scanf
add esp, 8
;;test to see if name was inputted
mov esi, szName
push esi
call _printf
add esp, 4
;;get the address of the second character in szName, which in my case is "a"
lea eax, [esi+1]
;; compare the second character with "a"
cmp eax, 0x61
je _works
;; if the character is not equal to "a"
push szNworks
call _printf
add esp, 4
push szPAUSE
call _system
add esp, 4
leave
ret
;;if the character is equal to "a"
_works:
push szworks
call _printf
add esp, 4
push szPAUSE
call _system
add esp, 4
leave
ret
【问题讨论】: