News:

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

how to insert opcodes directly into .code segment

Started by allynm, June 20, 2011, 06:26:00 PM

Previous topic - Next topic

jj2007


hutch--

Apart from encapsulating JWASM which it writes to disk, the source may be too BASIC for you. I wrote the tool in PB. To satisfy the author, JWASM was built from its source code with no modifications to the code then the binary was modified in the PE header using EDITBIN to convert it to a GUI application and thus, no console displayed. It reads the output log file to test if it worked correctly and converts the output binary to DB format if it was successful.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

allynm

Hi Hutch,

JJ is right.  Very cute indeed.  Out of curiosity, why did you write the output as decimal numbers rather than hex?  What is really cool about your "toy" is that it accepts multiple lines of assembly code. 

Thanks for sharing this!

Mark

hutch--

Mark,

The decimal format takes up less room than hex and its a consideration when you convert large amounts of data. i have used the tool from time to time and it works fine, you can do much the same directly from MASM if you put a label at the beginning and end of a procedure them write that binary data to disk, then you just convert the binary data to whatever format you like.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

allynm

Good morning, Hutch.

Thanks for the explanation on decimal/hex decision. 

Please forgive my naivete.  I'm trying to understand what you mean when you write:

Quoteyou can do much the same directly from MASM if you put a label at the beginning and end of a procedure them write that binary data to disk, then you just convert the binary data to whatever format you like

In thinking this technique thru--it interests me-- what I understand you to mean is that an arbitrary label like BEGIN:,,,,, STOP:   would be inserted at the beginning and end of the proc.  Then the proc would be assembled to, e.g. a COFF or OMF file.  Then some kind of OMF2HEX or COFF2HEX (or similar 2OTHER) program would convert the COFF or OMF format to something recognizable as ASCII hex or what have you.  Am I getting this right? 

If so, can you suggest an OMF or COFF converter program?  I've GOOGLED around to find one and am not successful.  I know there is Agner Fog's OBJCONV program (I've used it) but it provides much more than just a simple conversion into hex strings.


Thanks for all your help on this project.  And thanks as well to Mineiro and JJ2007.

Mark

jj2007

Quote from: allynm on June 23, 2011, 03:59:58 PM

In thinking this technique thru--it interests me-- what I understand you to mean is that an arbitrary label like BEGIN:,,,,, STOP:   would be inserted at the beginning and end of the proc.

See Reply #6, op_start: and op_end: labels. The proggie simply reads what is between the labels, and translates it to hex values. This works for opcodes (which is what you want anyway) but I have not tested it for code that is subject to relocation.

dedndave

Erol has a program named bin2coff.exe...
http://www.vortex.masmcode.com/

direct d/l link...
http://www.vortex.masmcode.com/files/bin2coff10.zip

you provide it with a filename (binary data) and a label name to be used as a public symbol

allynm

Hi 'Dave

Thanks.  I suppose I should have known Vortex or MichaelW would have done something like this.

Regards,
Marlk

dedndave

Edgar (donkey) may have something, too   :bg

hutch--

Mark,

If you want to perform this task manually you write your own proc in MASM then put a label at both the start and finish of the proc. hat gives you both the starting address and with a little arithmetic, the length in bytes.After you assemble the source code you then write the opcodes between the 2 labels directly to memory and convert it to either HEX or decimal output notation. The tool I posted uses JWASM to write a raw binary file from the opcodes you write into it which involves writing it to disk then converting it to the notation you require.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

dedndave

yah - as always, the more information we have to work with, the better the advice will be   :P

if we knew what you were trying to accomplish, you will get the best answers   :U

allynm

Hi Hutch & 'dave

Hutch -  thanks for expanding on your earlier post.  It was very helpful.

'dave - 
Quoteif we knew what you were trying to accomplish, you will get the best answers 

Assuming that this was directed towards me, and not Hutch, all I can say is:  as far as I can tell, everyone -- mineiro, jj, and Hutch-- understood exactly what I was seeking and each one came up with a different solution that works. 

The only problem I ran into was getting BIN2COFF.exe to work.  When I run the program on a test .asm file, it produces an odd-looking "hybrid" .obj that doesn't link successfully because the linker can't find mainCRTstartup.  I looked around on google to see if others had similar experiences but without success.  Maybe I don't understand what is meant by the phrase "any binary file" in the documentation for the program where the input file is described.

So, where I am now is trying out JJ's basic approach and following Hutch's explanation on how to use labels.

Thanks, everyone.

Mark

jj2007

One more for the road - suitable also for 16-bit code:

1. Insert 4-byte "label text" in your code, e.g. Ciao and Bye#, protected by a jmp:

Quote.Model small      ; credits to DednDave
.Stack 512
.686

.Data
MsgText   db "Hello 16-bit World", 13, 10, 10
   db "Hit any key to quit ...", 13, 10, "$"

.Code

_main proc FAR

; set the DS register to DGROUP (will fail with some Masm versions - use ml 6.15 or higher, or JWasm)
   mov ax, @DATA
   mov ds, ax

; display the message
   mov dx, offset MsgText
   mov ah, 9
   int 21h

   jmp @F
   
db "Ciao"
   push 123
   
fild word ptr [esp]
   fldpi
   fmulp
   fistp word ptr [esp]
   pop ax
   
db "Bye#"
@@:
; wait for a key
   mov ah, 0
   int 16h

; the DOS equivalent to ExitProcess
   mov ax, 4C00h
   int 21h

_main endp

end _main

2. Track those labels down:
Quoteinclude \masm32\MasmBasic\MasmBasic.inc   ; download
   Init
   Let
esi=FileRead$("Ciao16.exe")
   mov ecx, LastFileSize      ; MasmBasic knows that you need to know the size of this file ;-)
   .Repeat
      dec ecx
      mov eax, [esi+ecx]
   .Until Sign? || eax==Mirror$("Bye#")
   .if !Sign?
      lea ebx, [esi+ecx]         ; end address
      .Repeat
         dec ecx
         mov eax, [esi+ecx]
      .Until Sign? || eax==Mirror$("Ciao")
      .if !Sign?
         lea esi, [esi+ecx+4]         ; start address: first byte behind "Ciao"
         Open  "O", #1, "Opcodes.hex"         ; we write to a file
         Print #1, "TheOps", Tb$, "db "         ; some decoration
         .Repeat
            movsx ecx, byte ptr [esi]         ; get a single byte
            Let edi=Right$(Hex$(ecx), 2)+"h"   ; add the trailing h
            .if byte ptr [edi]>"9"            ; check if we need a leading zero
               Print #1, "0"
            .endif
            Print #1, edi
            inc esi
            .if esi<ebx
               Print #1, ", "               ; if it's not the last byte, we need a comma
            .endif
         .Until esi>=ebx
         Close
         Launch "NotePad.exe Opcodes.hex"   ; let's have a look
      .else
         Inkey "No start label found"
      .endif
   .else
      Inkey "No end label found"
   .endif
   Exit
end start

Oh... and by the way, the approach might look a bit basic, but it's pure Masm :bg

allynm

Hi jj-

I just had an opportunity to try out your first MasmBasic version.  Works fine for me!  I don't know BASIC, which is a bit limiting, I imagine.  It would be nice to be able to see the Notepad output arrayed in rows corresponding to the specific asm instructions.  But, that's just a quibble. 

I'll try the second version later today.  I kind of flip back and forth between 16-bit and 32-bit programs (confuses the daylights out of me, by the way) so something that works with 16 bits is very handy.

I'm still trying to figure out Hutch's method.  Haven't wasted my time in this pursuit because it has helped me learn some stuff about reading coff files I didn't know.

Regards,
Mark

dedndave

hiya Mark
here is an example of what Hutch is talking about...