News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

About Strings.

Started by G`HOST, December 26, 2005, 06:19:37 PM

Previous topic - Next topic

G`HOST

Hi,
   I have been learning and doing a lot of work on APIs and related stuff.But now i have started to take assembly as an language ,more seriously.And here arise a problem.I am trying to access individual members of string,separtely.And then Put it in an editbox.

String db "abcdef",0

so what should i be doing to access,a,b,c,d,e and f separetly?
lea as i know loads the address of the string.
So (lea eax,String+2) does points to the specified "member"but as understandably it (eax) points to the array starting at String+2.So it doesnt solve my problem.I tried AoA but it uses HLA syntexs which further confuse me.(And this problem is preventing me from understanding String reversing threads in the forum).So can someone help me in this,I understand the basic stuff.So plz dont direct me toward some tutorial , I have done a lot of searching and reading but i kepp getting more and more confused. And plz be a bit specific.Thank you.

shuttlebug

 mov esi,offset string
xor ebx,ebx
mov al,[esi+ebx]

use ebx as a base register and esi will always point to the beggining of your string

Vortex

Maybe, this very quick example can give you an idea :
.386
.model flat,stdcall
option casemap:none

include     \masm32\include\windows.inc
include     \masm32\include\kernel32.inc
include     \masm32\include\user32.inc
include     \masm32\include\masm32.inc

includelib  \masm32\lib\kernel32.lib
includelib  \masm32\lib\user32.lib
includelib  \masm32\lib\masm32.lib

.data
string  db "abcdef",0
crlf    db 13,10,0

.data?
temp    dd ?

.code

start:

    mov     esi,OFFSET string
@@:
    movzx   eax,BYTE PTR [esi]
    or      eax,eax
    jz      @f
    mov     temp,eax
    invoke  StdOut,ADDR temp
    invoke  StdOut,ADDR crlf
    inc     esi
    jmp     @b
@@:
    invoke  ExitProcess,0
   
END start

MichaelW

And another example:

; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
    include \masm32\include\masm32rt.inc
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
    .data
      string db "abcdef",0
    .code
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
start:
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
    mov   al, string
    invoke crt_printf,chr$("%c%c"),eax,10
    mov   al, string+5
    invoke crt_printf,chr$("%c%c%c"),eax,10,10

    mov   ebx, OFFSET string
  @@:
    mov   al, [ebx]
    or    al, al             ; test for terminating null
    jz    @F
    invoke crt_printf,chr$("%c%c"),eax,10
    inc   ebx
    jmp   @B
  @@:

    inkey "Press any key to exit..."
    exit
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
end start


a
f

a
b
c
d
e
f
Press any key to exit...


For a string defined in the data segment it would probably be better to use OFFSET to get the address of the string. LEA will work, but for at least for some processors it will be slower than a:

mov  reg, offset var

That actually assembles to:

mov  reg, immediate


eschew obfuscation

G`HOST

hi,Thanx for the input,Its not that hard as i thought at the first place ,anyhow I have seen a lot of codes dealing with arrays,using esi as a base pointer,is it mandatory to use it this way,since in AoA codes esi has been used as index and and ebx as Pointer to the base address.And what about edx and ecx,cant we use them ?

This is taken from AoA chapter on Arrays.
""Of course the more accesses to SixteenInts you make without reloading EBX, the greater your savings will be""
The code above lines are mentioning is:
lea( ebx, SixteenInts );
mov( index, esi );
mov( [ebx+esi*4], eax );

and

mov( index, ebx );
mov( SixteenInts[ ebx*4 ], eax );


Is there a difference between the two in terms of speed and memory,or I didnt understand the author properly.

Parse

Hey, I didn't want to start a new topic for something so small so I'll just put it here since it's related to strings.


string1 db "test", 0
string2 db "ing", 0
worked db "yes!", 0

code:
lea esi, string2
lea edi, string1

add edi, 4
mov ecx, FFFFFFh
repnz movsb

sub edi, 7


After I run this, edi does in fact point to the merged string "testing". However, esi doesn't point to worked. Shouldn't repnz movsb stop execution after it moves a zero? Or else, when is the zero flag set during movsb?

MichaelW

REPNZ is not a legal prefix for MOVS. A REP MOVSB will just move ECX bytes, adjusting ESI and EDI automatically as the move progresses.

eschew obfuscation

zooba

Quote from: G`HOST on December 27, 2005, 12:17:29 PM
is it mandatory to use it this way... esi has been used as index and and ebx as Pointer to the base address

Once upon a time it probably was (back in 16-bit land  :P :wink) but the current IA32 specifications (which define 32-bit assembly language for PC's) allow any registers to be used with any mnemonic - the encodings are generalised to allow it.

The only exceptions are the 'obsolete'/legacy string functions LODS, MOVS, CMPS, SCAS and STOS, which have defined uses for ESI, EDI, ECX and EAX, and the integer multiplication and division operations.