1

this is my code wrote in NASM

This code is find string s2 in string s1, return index if found or return -1 if not found.

My trouble is my EBX (index), it increase 1,2,3,..9 ok but when inc to 10, they will show ':' or 11 it show ';'

I don't know why? Please help

Many thanks

Sorry for my bad English

; 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
  • http://asciitable.com/ (`'0' + 10 = ':'`) – Ped7g May 24 '17 at 15:23
  • Thanks, how can i fix? – Đinh Văn Kiệt May 24 '17 at 15:25
  • Well, to display "10" on screen you must output two characters, digit 1 and digit 0. So you need to convert the integer value to string. Here is the same question with DOS platform target (so you would have to change OS service calls for char output for linux 64b `syscall`, and you can use 32/64b registers where needed): https://stackoverflow.com/q/40503119/4271923 or use google, there are many questions+answers like this on SO, but I'm never lucky to find proper one through SO search, you should have more luck through google and adding specific keywords. Also search for tutorial/book. – Ped7g May 24 '17 at 15:31
  • http://asmtutor.com/#lesson11 (linux tutorial, but unfortunately 32 bit) BTW, this is tiny bit advanced thing. If you are completely new to x86-64 assembly, just starting, you can ignore output to screen at the moment, and rather use debugger to check the values in registers/memory directly. You will need this skill later to fix bugs in your code, so learn to use debugger early (**now**). Also watching how the instruction does execute, step by step, and comparing that with instruction description will accelerate your learning of assembly a lot, as you will better understand how CPU works. – Ped7g May 24 '17 at 15:42
  • An old trick I used to use (not having a debugger) was to write registers to memory, write the memory to stdout, and pipe the program to hexdump. – Joshua Jun 01 '17 at 02:34

1 Answers1

1

The problem is you are displaying a number as a character.

add ebx, '0'

is a good way to convert a digit to a character for display. It is a bad way to convert a number to a character for display.

You want the following:

; 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
Joshua
  • 40,822
  • 8
  • 72
  • 132