global _main
extern _printf
section .data
fmt: db "%i", 0
fmt2: db "%s", 0
str1: db "hello world", 0
found: db "found char", 0
section .text
_main:
mov al, [str1+0]
cmp al, 'e'
je founds
push dword eax
push fmt
call _printf
add esp, 8
ret
founds:
push found
push fmt2
call _printf
add esp, 8
ret
nasm -fwin32 mul.asm -o mul.obj && gcc mul.obj && a.exe
Nie wiem, czy to jest do końca poprawne, ale mniej więcej tak trzeba napisać.
mov al, [str1+0]
, pod indeksami masz litery. mov al, [str1+1]
=> 'e'. Dlaczego +1, a nie np. +2, +4, +8, itd? A no dlatego, że bierzemy jeden bajt, czyli przesuwamy wskaźnik o jeden bajt w jakąś stronę i wtedy dostajemy bajt, który jest jakoś zakodowany, dla 'a' będzie to 97, czyli "01100001" bitowo.
Jeśli nie możesz skorzystać z rejestru 8. bitowego, to możesz wyzerować 3 górne bajty, a 4. (czwarty) zostawić.
global _main
extern _printf
section .data
fmt: db "%i", 0
fmt2: db "%s", 0
str1: db "hello world", 0
found: db "found char", 0
section .text
_main:
xor esi, esi
mov edi, [str1+1]
and edi, 0x000000FF
cmp edi, 'e'
je founds
push dword edi
push fmt
call _printf
add esp, 8
ret
founds:
push found
push fmt2
call _printf
add esp, 8
ret
Jak widać instrukcja and wyzerowała 3 bajty. 0x00, to jeden bajt. Bajt zerowy, to 0x00 w hex, a 0b00000000 bin. Najczęściej tak jest zakodowany koniec stringa, więc jeśli chcesz sprawdzić koniec stringa, to musisz porównać coś właśnie z bajtym zerowym.