Author Topic: Save code to binary.  (Read 903 times)

hutch--

  • Administrator
  • Member
  • ******
  • Posts: 5658
  • Mnemonic Driven API Grinder
    • The MASM32 SDK
Save code to binary.
« on: February 09, 2018, 04:25:31 AM »
A simple toy to perform a useful task.

; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

    include \masm32\include64\masm64rt.inc

    .data
      prclen dq glbl1 - glbl0               ; static calculate procedure length
      ofile db "filename.bin", 0            ; output file name string
      pfile dq ofile                        ; pointer to output file name string

    .code

; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

entry_point proc

    LOCAL bwrt  :QWORD                      ; bytes written variable

    lea rax, glbl0                          ; load address of 1st global label

    mov bwrt, savefile(pfile,rax,prclen)    ; write proc to disk at calculated length

    conout pfile," written to disk at ", \
                 str$(bwrt)," bytes",lf     ; display result

    waitkey                                 ; pause so you can see result

    .exit                                   ; close app

entry_point endp

; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

 NOSTACKFRAME

 getbin proc

  glbl0::

  ; -------------------------------

    mov r11, rsi                            ; preserve rsi and rdi
    mov r10, rdi

    mov rsi, rcx                            ; source address in rcx
    mov rdi, rdx                            ; destination address in rdx
    mov rcx, r8                             ; byte count to copy in r8
    rep movsb

    mov rsi, r11                            ; restore rsi and rdi
    mov rdi, r10

    ret

  ; -------------------------------

  glbl1::

 getbin endp

 STACKFRAME

; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

 ;     This is the above procedure converted from saved binary to hex notation
 ;
 ;  NOSTACKFRAME
 ;
 ;  bcopy proc
 ;
 ;     db 04Ch,08Bh,0DEh,04Ch,08Bh,0D7h,048h,08Bh,0F1h,048h,08Bh,0FAh,049h,08Bh,0C8h,0F3h
 ;     db 0A4h,049h,08Bh,0F3h,049h,08Bh,0FAh,0C3h
 ;
 ;  bcopy endp
 ;
 ;  STACKFRAME

; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

    end
hutch at movsd dot com
http://www.masm32.com    :biggrin:  :biggrin: