Using PureBasic to Put the Hex Into ASM Code

Bare metal programming in PureBasic, for experienced users
oldefoxx
Enthusiast
Enthusiast
Posts: 532
Joined: Fri Jul 25, 2003 11:24 pm

Using PureBasic to Put the Hex Into ASM Code

Post by oldefoxx »

"Put the Hex into ASM Code"? Sounds strange, but let me show you what I mean

Here is the coded section:

Procedure.l tmp()
lablel1:
mov eax, 0xffffffff
label2:
add eax,1
label3:
ProcedureReturn
label4:
EndProcedure

MessageRequester("TRIAL","EAX = "+str$(tmp))

I get the result of 0, which is what I should get. But now I want the hex code involved
I want to use a pointer to label1 and pull up each byte in sequence until I get to label2,
then go from a pointer to label2 and do the same again for each byte until I reach
label3, then again from lable3 until I reach label4.

What I am trying to do is see when and where the prefix 66h comes into play. According
to what I've read, it should not appear. Then I am going to change the instructions to
involve ax instead of eax, When i repeat the scan from label1 to label3, I expect to find
two. I've a reason for wanting to know this for sure, which is why I'm looking for a bit of
help on using label pointers.
has-been wanna-be (You may not agree with what I say, but it will make you think).
jack
Addict
Addict
Posts: 1336
Joined: Fri Apr 25, 2003 11:10 pm

Re: Using PureBasic to Put the Hex Into ASM Code

Post by jack »

oldefoxx, have a look at http://www.purebasic.fr/english/viewtop ... 13&t=51174
fasm dll comes with a demo that instantly assembles snippets of asm source giving you the machine code.
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3870
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: Using PureBasic to Put the Hex Into ASM Code

Post by wilbert »

Code: Select all

Procedure.l tmp()
label1:
!mov eax, 0xffffffff
label2:
!add eax,1
label3:
ProcedureReturn
label4:
EndProcedure

s.s = ""
*addr.Ascii = ?label2
While *addr < ?label3
  s + Hex(*addr\a) + " "
  *addr + 1
Wend

MessageRequester("Output" , s)
oldefoxx
Enthusiast
Enthusiast
Posts: 532
Joined: Fri Jul 25, 2003 11:24 pm

Re: Using PureBasic to Put the Hex Into ASM Code

Post by oldefoxx »

Very nice. I had a suspicion about how it would be approached, but I was not "enough with the language"
to bring it together. I made a few small changes, to get the flavor I was looking for:

Code: Select all

Procedure.l tmp()
    label1:
    !mov eax, 0xffffffff
    label2:
    !add eax,1
    label3:
    ProcedureReturn
    label4:
    EndProcedure

    s.s = "!mov eax, 0xffffffff ==> "
    *addr.Ascii = ?label1
    While *addr < ?label2
      s + Hex(*addr\a) + " "
      *addr + 1
    Wend
    s+Chr(13)+Chr(10)+"!add eax,1 ==> "
    While *addr< ?label3
      s+Hex(*addr\a)+" "
      *addr+1
    Wend
    s+Chr(13)+Chr(10)+"ProcedureReturn (EAX) = "+Str(tmp)
    MessageRequester("Output" , s)
But to verify that the process is going what is expected of it, I changed the value
being loaded into EAX at the start of the procedure. Now it reads like this:

Code: Select all

    Procedure.l tmp()
    label1:
    !mov eax, 0x12345678
    label2:
    !add eax,1
    label3:
    ProcedureReturn
    label4:
    EndProcedure

    s.s = "!mov eax, 0xffffffff ==> "
    *addr.Ascii = ?label1
    While *addr < ?label2
      s + Hex(*addr\a) + " "
      *addr + 1
    Wend
    s+Chr(13)+Chr(10)+"!add eax,1 ==> "
    While *addr< ?label3
      s+Hex(*addr\a)+" "
      *addr+1
    Wend
    s+Chr(13)+Chr(10)+"ProcedureReturn (EAX) = "+Str(tmp)
    MessageRequester("Output" , s)
No matter what value is assign to EAX at the start, the ProcedureReturn is always
equal to zero (0). Any idea why that is?
has-been wanna-be (You may not agree with what I say, but it will make you think).
User avatar
Demivec
Addict
Addict
Posts: 4086
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: Using PureBasic to Put the Hex Into ASM Code

Post by Demivec »

oldefoxx wrote:No matter what value is assign to EAX at the start, the ProcedureReturn is always
equal to zero (0). Any idea why that is?
It is because you never execute the procedure to get its return value, you only reference its contents (machine code) with the labels.

You should make this small change:

Code: Select all

;change this
s+Chr(13)+Chr(10)+"ProcedureReturn (EAX) = "+Str(tmp)

;to this
s+Chr(13)+Chr(10)+"ProcedureReturn (EAX) = "+Str(tmp())
oldefoxx
Enthusiast
Enthusiast
Posts: 532
Joined: Fri Jul 25, 2003 11:24 pm

Re: Using PureBasic to Put the Hex Into ASM Code

Post by oldefoxx »

Then I just learned something. tmp is not tmp(). Interesting. In some other languages,
if there is no parameters involved, the () is not required. An example that many people
might recognize where the () is required is the POS() in some of the early Basics, which
involves the current position on the line (column location). I also learned something
else, which is that while tmp is therefore undefined, it did not signal a compiler error.
has-been wanna-be (You may not agree with what I say, but it will make you think).
oldefoxx
Enthusiast
Enthusiast
Posts: 532
Joined: Fri Jul 25, 2003 11:24 pm

Re: Using PureBasic to Put the Hex Into ASM Code

Post by oldefoxx »

Now this is interesting. I set the code back to 0xffffffff as I had it originally, and changed
Str() in the later part to Hex(), and got back an answere with 16 F's in it. Eight would have
been enough. Sixteen is like a quad, not a long.

Code: Select all

    Procedure.l tmp()
    label1:
    !mov eax, 0xfffffffe
    label2:
    !add eax,1
    label3:
    ProcedureReturn
    label4:
    EndProcedure

    s.s = "!mov eax, 0xffffffff ==> "
    *addr.Ascii = ?label1
    While *addr < ?label2
      s + Hex(*addr\a) + " "
      *addr + 1
    Wend
    s+Chr(13)+Chr(10)+"!add eax,1 ==> "
    While *addr< ?label3
      s+Hex(*addr\a)+" "
      *addr+1
    Wend
    s+Chr(13)+Chr(10)+"ProcedureReturn (EAX) = "+Hex(tmp())
    MessageRequester("Output" , s)
has-been wanna-be (You may not agree with what I say, but it will make you think).
User avatar
Danilo
Addict
Addict
Posts: 3037
Joined: Sat Apr 26, 2003 8:26 am
Location: Planet Earth

Re: Using PureBasic to Put the Hex Into ASM Code

Post by Danilo »

oldefoxx wrote:Now this is interesting. I set the code back to 0xffffffff as I had it originally, and changed
Str() in the later part to Hex(), and got back an answere with 16 F's in it. Eight would have
been enough. Sixteen is like a quad, not a long.
Use Hex(tmp(),#PB_Long).

Hex() takes a quad by default, so the .l 0xffffffff (it is -1, because PB uses signed types)
is converted to .q 0xffffffffffffffff (-1 for quads).

RSet is used to fill the string on the left side, so the byte display "1" becomes "01"
and long "FF" becomes "000000FF".

Variables do not need to be defined by default. Use EnableExplicit for enabling it.

Yes, procedures are always used with (), for example the address of the procedure is: @tmp().
Without () it is a simple variable.

Code: Select all

    EnableExplicit
    
    Procedure.l tmp()
        label1:
            !mov eax, 0xfffffffe
        label2:
            !add eax,1
        label3:
            ProcedureReturn
        label4:
    EndProcedure
    
    Procedure.s GetByteCode( *_start.Ascii, *_end.Ascii, _msg.s="" )
        While *_start < *_end
            _msg + " " + RSet( Hex( *_start\a , #PB_Byte ) , 2, "0")
            *_start + 1
        Wend
        ProcedureReturn _msg
    EndProcedure

    Define s.s

    s = GetByteCode(?label1,?label2,"!mov eax, 0xfffffffe ==>")
    s + #CRLF$
    s + GetByteCode(?label2,?label3,"!add eax,1 ==>")
    s + #CRLF$ + "ProcedureReturn (EAX) = " + RSet( Hex(tmp(),#PB_Long) , 8, "0" )
    MessageRequester("Output" , s)
oldefoxx
Enthusiast
Enthusiast
Posts: 532
Joined: Fri Jul 25, 2003 11:24 pm

Re: Using PureBasic to Put the Hex Into ASM Code

Post by oldefoxx »

I appreciate your response. Stretches my understanding further. It's real late, and I
think I am done for the night. This has been a good thread to be part of.
has-been wanna-be (You may not agree with what I say, but it will make you think).
Post Reply