News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests

Main Menu

Access to the elements of a byte variable

Started by DaveG, April 15, 2013, 06:19:38 AM

Previous topic - Next topic

DaveG

Hey, It's me again, now I'm trying to see the first element of a list...like this:
sep byte 4,5,6,20
The thing is...if I do this:
mov esi,offset sep
mov ecx,[esi+1]
printf("%c ",ecx)

I'm expecting to get "5" or something like that.
But somehow...i just have : ♣
If I use printf("%d", ecx) I get... 460293.

I'm not really sure I know what the problem is, I need to have access to 4,5,6,20 since they mean the number of characters I have to extract from certain word...Like "HolaHowAreYou?" I want the 4 to extract "Hola" and so on...

jj2007

You need to convert the byte to a string:

include \masm32\include\masm32rt.inc

.code
sep byte 4,5,6,20

start:   mov esi, offset sep
   movzx ecx, byte ptr [esi+1]
   inkey str$(ecx), " found"
   exit
end start


Look up movzx in \Masm32\help\opcodes.chm
byte ptr is necessary to tell the assembler what you need (i.e. a byte, not a word)

dedndave

mov ecx,[esi+1]
that will load ECX with a dword, starting at [ESI+1]
as Jochen mentioned, you really want a single byte

"♣" is the ASCII character for a binary 5
the ASCII decimal numbers are 30h("0") to 39h("9")
so, if you ADD 30h (or use OR), it will convert the binary 5 to an ASCII 35h, which is "5"
        add     cl,30h
or
        or      cl,30h
now, you need to store that back into memory to form a string
the display macros and functions aren't really designed to display chars directly from a register

DaveG

Quote from: jj2007 on April 15, 2013, 07:13:14 AM
You need to convert the byte to a string:

include \masm32\include\masm32rt.inc

.code
sep byte 4,5,6,20

start:   mov esi, offset sep
   movzx ecx, byte ptr [esi+1]
   inkey str$(ecx), " found"
   exit
end start


Look up movzx in \Masm32\help\opcodes.chm
byte ptr is necessary to tell the assembler what you need (i.e. a byte, not a word)
I haven't really tested this since my last purpose is not to print the number.
Quote from: dedndave on April 15, 2013, 08:11:19 AM
mov ecx,[esi+1]
that will load ECX with a dword, starting at [ESI+1]
as Jochen mentioned, you really want a single byte

"♣" is the ASCII character for a binary 5
the ASCII decimal numbers are 30h("0") to 39h("9")
so, if you ADD 30h (or use OR), it will convert the binary 5 to an ASCII 35h, which is "5"
        add     cl,30h
or
        or      cl,30h
now, you need to store that back into memory to form a string
the display macros and functions aren't really designed to display chars directly from a register
Now this works better for my objective.

I was just trying to print it so I could see wether I was getting the right number or not.
Now, I use this:
   
        mov esi,offset sep ;
mov cl,[esi]
add cl,030h  ;This gives me the first number which is 4.
mov esi,offset c1 ;Now I point esi to my c1 string "Holahowareyou"
movzx ecx,cl ; This is supposed to move [some zeroes]...04 into my ecx registry, right?
segPal: ;loop to print the first 4 characters of c1.
mov eax,[esi] ;This should move the first character from c1 to eax, isnt't it?
inc esi ;I 'move' one char to the right.
pushad
printf("%c",eax) ;Failure :( It prints the whole c1 string with the inkey text and some other characters. Then Windows detects a problem and has to close the .exe
popad
loop segPal


EDIT: jj2007  solution actually gives me what I want, thanks pal!