Author Topic: NASM .EXE OUTPUT  (Read 10150 times)

Offline mahmoud899

  • Jr. Member
  • *
  • Posts: 2
NASM .EXE OUTPUT
« on: August 28, 2012, 03:51:56 AM »
.model small
.stack 64
.code
main proc far
        mov ax, 33H
        mov bh, 33H
        add ax, bh
main endp
end main

I want to assemble this code with nasm and I want the output to be .exe, How do I do that, and what commands should I use to output the .exe
Thank You

Offline Bryant Keller

  • Forum Moderator
  • Full Member
  • *****
  • Posts: 360
  • Country: us
    • About Bryant Keller
Re: NASM .EXE OUTPUT
« Reply #1 on: August 28, 2012, 07:08:10 AM »
It can't be done.... that's not valid NASM source, it's MASM source.

About Bryant Keller
bkeller@about.me

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: NASM .EXE OUTPUT
« Reply #2 on: August 28, 2012, 12:56:03 PM »
Code: [Select]
add ax, bh
... not even valid x86 instructions, so Masm won't assemble it either. If you change it to:
Code: [Select]
add ax, bx
Masm would probably assemble it... or Jwasm would probably be an alternative (some people don't like MS!). That'll only give you a linkable .obj file, you'll need a linker to link it to an .exe, and then it'll almost certainly crash because you haven't provided an exit back to your OS (unless "endp" generates code to do it... which I doubt).

The simplest way to get an executable from Nasm would be to assemble it to a .com file, not an .exe. Nasm will do this without the assistance of a linker.
Code: [Select]
; nasm -f bin myprog.asm -o myprog.com
bits 16 ; default
org 100h ; tell Nasm where DOS will load us
section .text ; default
    mov ax, 33h
    mov bh, 33h
    add ax, bx
    ret ; exit to DOS

If you really need this to be an .exe (different executable format)...
Code: [Select]
; nasm -f obj myprog.asm
; alink -oEXE myprog.obj
; or other linker...

segment stack stack
    resb 64

segment code ; any name will do
    ..start: ; entrypoint
    mov ax, 33h
    mov bh, 33h
    add ax, bx
   
    mov ah, 4Ch ; exit to DOS
    mov al, 0 ; exit code
    int 21h

Note that Masm/Tasm/Jwasm designate the entrypoint with "end main", Nasm uses the special symbol "..start". Also, note that if you had a "data" segment ("hello world" in it, perhaps), you would have to set up ds (and perhaps es too) to point to it. DOS does not do this for an .exe! (but does for a .com file - another reason why a .com file is easier)

If you'd like to display the results of your addition (which implicitly ASSumes that bx is zero on startup - "probably" true), that'll require some more code...

Best,
Frank


Offline mahmoud899

  • Jr. Member
  • *
  • Posts: 2
Re: NASM .EXE OUTPUT
« Reply #3 on: August 30, 2012, 12:37:01 PM »
Thank You for the reply, its alot of help.

Thank You