Author Topic: Nasm dissassemble and assemble(compile)  (Read 25974 times)

Offline Teol

  • Jr. Member
  • *
  • Posts: 12
Nasm dissassemble and assemble(compile)
« on: July 25, 2016, 07:26:54 PM »
Hello,
How can i dissassemble linux program and compile it back again using the disassembled code?
I know i can use ndisasm -b 32 [program] > file ,but the output is such that i cannot compile it back to what it was.
How could i compile it back?

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: Nasm dissassemble and assemble(compile)
« Reply #1 on: July 25, 2016, 08:32:00 PM »
Well, you can write yourself a little utility to remove the first 20(?) characters from each line. You might be able to reassemble this. Useless, since you can't modify it at all.

I would suggest you look at Agner Fog's "objconv". Jeff Owens has a utility that will convert to source also. In general, this is not a very practical thing to do. You want a program, write it.

Best,
Frank


Offline fixit

  • Jr. Member
  • *
  • Posts: 23
Re: Nasm dissassemble and assemble(compile)
« Reply #2 on: August 02, 2016, 10:54:53 PM »
Good to see you around. :-)

I have nasm installed in Ubuntu.

Will need a lot of help.

Am used to MASM which is GUI.

Every example in the forum yields this message ??

Code: [Select]
Label or instruction expected at start of line
« Last Edit: August 03, 2016, 01:16:51 AM by fixit »

Offline fixit

  • Jr. Member
  • *
  • Posts: 23
Re: Nasm dissassemble and assemble(compile)
« Reply #3 on: August 03, 2016, 06:08:22 PM »
Anyone home ?

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: Nasm dissassemble and assemble(compile)
« Reply #4 on: August 03, 2016, 06:20:43 PM »
Seems improbable that "every example" gives that message. What are you doing, exactly?

Best,
Frank


Offline fixit

  • Jr. Member
  • *
  • Posts: 23
Re: Nasm dissassemble and assemble(compile)
« Reply #5 on: August 04, 2016, 10:53:02 PM »
Code: [Select]
; nasm -fwin32 -oWinMain.obj WinMain.asm
; golink /entry start /fo WinMain.exe WinMain.obj kernel32.dll

;
; WinMain.asm
; 06/23/2014
;
; Assembler:    NASM version 2.11.05 compiled on May 21 2014
; Linker:       GoLink.Exe Version 1.0.0.0
;
; nasm -fwin32 -oWinMain.obj WinMain.asm
; golink /entry start /fo WinMain.exe WinMain.obj kernel32.dll
;
cpu     386
global  start
extern  GetCommandLineW
extern  GetModuleHandleW
extern  ExitProcess

[SECTION .code use64]

start:
        push    3           ; nCmdShow SW_MAXIMIZE who needs this argument????????
        call    GetCommandLineW ; win32 API
        push    eax             ; lpCmdLine
        xor     eax, eax
        push    eax             ; hPrev nobody need this argument.
        push    eax
        call    GetModuleHandleW; win32 API
        push    eax             ; hInst
        call    WinMain         ; call WinMain with 4 arguments on the stack
exit:
        push    eax             ; error code
        call    ExitProcess     ; win32 API
; WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR lpCmdLine, int nCmdShow)
WinMain:
        ; do some useful things here
        xor eax, eax    ; return 0, everything went well
ret 16  ; restore the stack, pop 4 arguments


Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: Nasm dissassemble and assemble(compile)
« Reply #6 on: August 05, 2016, 12:01:56 AM »
Well...
[SECTION .code use64]
seems unlikely to play nicely with "-f win32"! In fact, "use64" may not assemble a "push" of a 32-bit register. Why is that there? ".code" is not one of the "known" section names in "-f win32", but I would not expect that to cause an error.

Given that you're getting errors in other examples(?), it might be a problem with your editor not producing ascii text. I should have thought of that in the first place - some 'doze editors are known to have this issue, but you should be able to tell it to produce ascii. I suggested this on c.l.a.x. the other day. Have you looked into it? (somewhere in the "properties" heirarchy, I suspect?)

Best,
Frank


Offline fixit

  • Jr. Member
  • *
  • Posts: 23
Re: Nasm dissassemble and assemble(compile)
« Reply #7 on: August 05, 2016, 01:27:30 AM »
I am using geany as my text editor and I believe it saves as ascii text.


Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: Nasm dissassemble and assemble(compile)
« Reply #8 on: August 05, 2016, 03:25:35 AM »
I'm not familiar with geany, but I think it should be alright. I pasted your file into Linux and it assembled without a peep for me. (I tried it with -f win64 and it does complain about the 32-bit "push"s but -f win32 is fine) We don't have source code for "golink" so I can't try linking it - wouldn't run anyway. I have no clue what your problem might be. Hope a Windows user comes by and can help you with it.

Best,
Frank


Offline fixit

  • Jr. Member
  • *
  • Posts: 23
Re: Nasm dissassemble and assemble(compile)
« Reply #9 on: August 05, 2016, 05:01:35 AM »
Can you direct me to some nasm code that will run on Linux ?


Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: Nasm dissassemble and assemble(compile)
« Reply #10 on: August 05, 2016, 07:08:55 AM »
That byte editor you were looking at is great! (just type "make") There's lots in the "examples" section (tho some dead links). I've got lots of 32-bit code. Here's something simple to start with...

Code: [Select]
; nasm -f elf hw2u.asm
; ld -o hw2u hw2u.o -melf_i386


global _start

MAXNAME equ 256

section .text
_start:

    nop ; for the debuggers
commence: ; ditto
    mov ecx, prompt
    mov edx, prompt_len
    call write_stdout
   
    mov eax, 3 ; __NR_read
    mov ebx, 0 ; stdin
    mov ecx, namebuf ; buffer
    mov edx, MAXNAME ; maximum to read
    int 80h

    dec eax ; length returned includes LF we don't want
    push eax ; save it for later
   
    mov ecx, greet
    mov edx, greet_len
    call write_stdout
   
    mov ecx, namebuf
    pop edx ; retrieve the length
    call write_stdout
   
    mov ecx, coda
    mov edx, coda_len
    call write_stdout
   
exit:
    mov eax, 1 ; __NR_exit
    int 80h

write_stdout:
    push ebx
    mov eax, 4 ; __NR_write
    mov ebx, 1 ; stdout
    int 80h
    pop ebx
    ret


section .data

    prompt db "Please tell me your name?  "
    prompt_len equ $ - prompt

    greet db "Hello, "
    greet_len equ $ - greet
   
    coda db "! Welcome to Linux Assembly!", 10
    coda_len equ $ - coda

section .bss

    namebuf resb MAXNAME


What sort of thing are you looking for?

Best,
Frank


Offline fixit

  • Jr. Member
  • *
  • Posts: 23
Re: Nasm dissassemble and assemble(compile)
« Reply #11 on: August 05, 2016, 07:15:16 AM »
Thanks.

How do i run hw2u in Linux ?

I am looking for GUI code.

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: Nasm dissassemble and assemble(compile)
« Reply #12 on: August 05, 2016, 08:09:08 AM »
You type "./hw2u". Then you type "uhClem" or whatever you claim your name is.

In Windows the GUI is part of the OS. In Linux, it is a separate program which functions as a server. At the lowest level we send it requests to do stuff on-screen through a socket - just as if we were talking to the internet. It is extremely tedious to do anything at this level.  Something like "MessageBoxA" would be a big deal. We can use "libX" to take care of some of the housekeeping, but that's still too low-level and "helper" libs are used. "GTK" or so. We can call these libs from assembly, but there is no advantage to doing so. It is just as efficient and probably much easier (depending on your taste) to do it from C or higher. As such, I have very little Linux GUI code to share with you (can find some...). That's the sad story on that...

Best,
Frank


Offline fixit

  • Jr. Member
  • *
  • Posts: 23
Re: Nasm dissassemble and assemble(compile)
« Reply #13 on: August 05, 2016, 08:26:40 AM »
You type "./hw2u". Then you type "uhClem" or whatever you claim your name is.

In Windows the GUI is part of the OS. In Linux, it is a separate program which functions as a server. At the lowest level we send it requests to do stuff on-screen through a socket - just as if we were talking to the internet. It is extremely tedious to do anything at this level.  Something like "MessageBoxA" would be a big deal. We can use "libX" to take care of some of the housekeeping, but that's still too low-level and "helper" libs are used. "GTK" or so. We can call these libs from assembly, but there is no advantage to doing so. It is just as efficient and probably much easier (depending on your taste) to do it from C or higher. As such, I have very little Linux GUI code to share with you (can find some...). That's the sad story on that...

Best,
Frank

My real name is Andy.

Your example compiled fine and ran.

Many thanks for your help. :-)

Offline Bryant Keller

  • Forum Moderator
  • Full Member
  • *****
  • Posts: 360
  • Country: us
    • About Bryant Keller
Re: Nasm dissassemble and assemble(compile)
« Reply #14 on: August 11, 2016, 03:52:35 AM »
I am looking for GUI code.

Linux users tend to be more comfortable running programs from the command line than Windows users are. In general, it's an accepted practice to write a command line version of your program, then later on add a graphical wrapper in a scripting language like Python/Tk. That said, if you're determined to write GUI programs you could avoid a lot of the complication by using Gtk's Glade library. The development process with Gtk/Glade is to use the WYSIWYG form developer called GtkBuilder to develop your user interface, then write initialization routines and event handlers in your code. This is about as easy as VB.Net coding for GUI's.

Glade - A user interface designer
Cross OS App For Linux/Windows Using GTK <- with NASM

About Bryant Keller
bkeller@about.me