Re: Binary conversion (machine)

new topic     » goto parent     » topic index » view thread      » older message » newer message

Don <Graebel at hotmail.com> wrote:

> Actually I got bored and started optimizing the byte
> swap routine the easy way.  Directly.  However for
> some reason it just doesnt seem to be working quite
> right.

> Someone whos boned up on machine to Eu interfacing
> want to take a peek at this and see what I did
> wrong?

> ==========
> include machine.e

> atom mem, SwapBytes
> sequence Code_Space

> mem = allocate( 4 )
> poke4( mem, 90 )

> Code_Space =
> {
>     #50,                        -- push eax
>     #B8} & int_to_bytes(mem) &  -- mov eax, offset mem
> {   #0F, #C8,                   -- bswap eax
>     #A3} & int_to_bytes(mem) &  -- mov offset mem, eax
> {   #58,                        -- pop eax
>     #C3                         -- ret
> }

> SwapBytes = allocate( length(Code_Space) )
> poke( SwapBytes, Code_Space )

> ? peek4u( mem )
> call( SwapBytes )
> ? peek4u( mem )
> call( SwapBytes )
> ? peek4u( mem )
> call( SwapBytes )
> ? peek4u( mem )

> free( SwapBytes )
> free( mem )

> -- break for debugging
> atom break
> ==========

> Don Phillips


The following code works for me:

--------------------------------------------------------
include machine.e

atom mem
mem = allocate(4)

constant SwapBytes = allocate(15)
poke(SwapBytes, {
    #50,                    --    0: push eax
    #A1,#00,#00,#00,#00,    --    1: mov eax, [mem] (2)
    #0F, #C8,               --    6: bswap eax
    #A3,#00,#00,#00,#00,    --    8: mov [mem], eax (9)
    #58,                    --    D: pop eax
    #C3})                   --    F: ret
poke4(SwapBytes + 2, mem)
poke4(SwapBytes + 9, mem)

poke4(mem, #90)
-- poke4(mem, #12345678)  -- better for demonstration

printf(1, "%08x\n", peek4u(mem))
call( SwapBytes )
printf(1, "%08x\n", peek4u(mem))
call( SwapBytes )
printf(1, "%08x\n", peek4u(mem))
call( SwapBytes )
printf(1, "%08x\n", peek4u(mem))

free(SwapBytes)
free(mem)
--------------------------------------------------------


When I first tried to compile the asm code with Pete
Eberlein's ASM, I got the following error message:

  ---------------------------------
  ASM to Euphoria compiler (32-bit)
  by Pete Eberlein    Dec 29, 2001
  ---------------------------------
  Line 4: Not a valid instruction 'bswap'

Mmmm, must be "Line 3" ...

Anyway, I had to compile the asm code without the "bswap"
instruction, and insert the opcode manually.
Don, how did you get the opcode for "bswap eax"? Is there
a newer version of ASM.EX?

Regards,
   Juergen

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu