这是我在NASM写的代码
此代码在字符串s1中找到字符串s2,如果找到则返回索引,如果未找到则返回-1。
我的麻烦是我的EBX(索引),它增加1,2,3,... 9 ok但是当inc到10时,他们会显示':'或者它显示';'
我不知道为什么?请帮忙
非常感谢
抱歉我的英文不好
; DINH VAN KIET
; TUAN 3
; Find String 32bit
; Build
; nasm -f elf find_string_32bit.asm -o find_string_32bit.o ; file object will be created
; ld -m elf_i386 -o find_string_32bit find_string_32bit.o ; link the object file and create executable file
; MO TA
; Tim chuoi s2 trong chuoi s1
SYS_EXIT EQU 1
SYS_READ EQU 3
SYS_WRITE EQU 4
STDIN EQU 0
STDOUT EQU 1
%define LEN 64 ; do dai toi da
%define LEN_STT 2
segment .data
msgS1 db 'Nhap chuoi s1: '
msgS1Len equ $ - msgS1
msgS2 db 'Nhap chuoi s2 (can tim): '
msgS2Len equ $ - msgS2
pos dw 00
segment .bss
s1 resw LEN
s2 resw LEN
temp resb 1
segment .learn
global _start
_start:
mov edx,msgS1Len
mov ecx,msgS1
call display
mov edx,LEN
mov ecx,s1
call read
mov edx,msgS2Len
mov ecx,msgS2
call display
mov edx,LEN
mov ecx,s2
call read
; dung eax de xu ly chuoi s2
mov eax,[s2]
mov edx,0 ; dung edx lam index
; dung ecx de xu ly chuoi s1
mov ecx,s1
mov ebx,-1 ; dung ebx de dem
lap1:
; kiem tra het chuoi
cmp byte[ecx],0
je done
; duyet tung ki tu cua s2
mov eax,[s2+edx]
mov [temp],eax
; de test
;push ebx
;push edx
;push ecx
;mov edx,1
;mov ecx,temp
;call display
;pop ecx
;pop edx
;pop ebx
; dua cac ki tu vao thanh ghi al va ah de so sanh
mov al,byte[ecx]
mov ah,[temp]
cmp ah,0xA ; kiem tra xem da het chuoi s2 hay chua?
je done
inc ebx ; tang bien vi tri
inc ecx ; tang index cua ecx
cmp al,ah ; kiem tra xem ki tu co khop nhau hay ko?
je meet
jne miss
jmp lap1
; Neu khop
meet:
cmp edx,0
jne met
push ebx
inc edx ; tang bien edx de kiem tra tiep s2
jmp lap1
; Neu tiep tuc khop
met:
inc edx
jmp lap1
; Neu khong khop
miss:
mov edx,0 ; dua edx ve dau s2 de kiem tra lai
jmp lap1
; Neu tim thay
found:
; in ra vi tri tim thay
pop ebx
add ebx,'0'
jmp exit
; Neu khong tim thay
notfound:
; in ra -1
mov ebx,'-1'
jmp exit
done:
cmp edx,0
je notfound
jne found
exit:
mov [pos],ebx
mov edx,2
mov ecx,pos
call display
mov eax,SYS_EXIT
int 80h
display:
mov eax,SYS_WRITE
mov ebx,STDOUT
int 80h
ret
read:
mov eax,SYS_READ
mov ebx,STDIN
int 80h
ret
答案 0 :(得分:1)
问题是你是将一个数字显示为一个字符。
add ebx, '0'
是将数字转换为字符以进行显示的好方法。将数字转换为字符进行显示是一种糟糕的方法。
您需要以下内容:
; variable in ebx
itoa:
mov eax, ebx
mov ecx, 10
mov esi, buf + 10
xor edx, edx
.nxt
div ecx
add dl, '0'
dec esi
mov [esi], dl
or eax, eax
jnz .nxt
mov edx, buf + 10
sub edx, esi
ret
; pointer in esi, length in edx
;... (bss area)
buf resb 10