Author Topic: Access to the elements of a byte variable  (Read 2706 times)

DaveG

  • Guest
Access to the elements of a byte variable
« on: April 15, 2013, 06:19:38 AM »
Hey, It's me again, now I'm trying to see the first element of a list...like this:
Code: [Select]
sep byte 4,5,6,20The thing is...if I do this:
Code: [Select]
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

  • Member
  • *****
  • Posts: 10096
  • Assembler is fun ;-)
    • MasmBasic
Re: Access to the elements of a byte variable
« Reply #1 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)

dedndave

  • Member
  • *****
  • Posts: 8825
  • Still using Abacus 2.0
    • DednDave
Re: Access to the elements of a byte variable
« Reply #2 on: April 15, 2013, 08:11:19 AM »
Code: [Select]
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"
Code: [Select]
        add     cl,30hor
Code: [Select]
        or      cl,30hnow, 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

  • Guest
Re: Access to the elements of a byte variable
« Reply #3 on: April 15, 2013, 08:41:56 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.
Code: [Select]
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"
Code: [Select]
        add     cl,30hor
Code: [Select]
        or      cl,30hnow, 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:
   
Code: [Select]
        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!