Feeds:
Posts
Comments

Archive for January 2nd, 2019

In this tutorial, I will show you how to count the string length.
Type the codes below and save it to ‘stringlen.asm’.
1 section .text
2 global _start
3 _start:
4
5 mov eax,msg ;copy msg address pointer to eax register
6 mov ebx,eax ;copy to ebx register
7
8 nextchar: ;LOOP
9 cmp byte [eax],0 ;check if reach 0h character
10 jz done ;if reach go to done:
11 inc eax ;if not reach, increase eax pointer by 1
12 jmp nextchar ;jump back to next character:
13 done:
14 sub eax,ebx ;after all done. eax will has the last address of msg memory
15 ;by subtitute eax with ebx (eax=eax-ebx)
16 ;from the beginning msg memory you will get
17 ;total number of characters.
18 mov ebx,eax ;copy the total number to ebx register so you can display it
19 ;using ‘$echo $?’ command.
20 mov eax,1 ;exit
21 int 0x80
22
23 section .data
24 msg db ‘12345678901234567890’,0h

Compile and run it.
$ nasm -f elf32 stringlen.asm -o stringlen.o && ld stringlen.o -o stringlen
$ ./stringlen
To display the return value of the program, you can type: echo $?
$ echo $?
20
$

 

Read Full Post »