Author Topic: Problem with first program for NASM  (Read 6388 times)

Offline KrukersRadek

  • Jr. Member
  • *
  • Posts: 5
Problem with first program for NASM
« on: January 12, 2012, 11:51:01 PM »
Code: [Select]
use16
 bits 16 ;for 16 bits program

 segment data

 hello: db 'hello, world','$'

 segment _stack stack
 resb 64
 stacktop:


 segment code

 ..start:
 mov ax,data
 mov ds,ax
 mov ax,_stack
 mov ss,ax
 mov sp,stacktop

 mov dx,hello
 mov ax,0900h
 int 21h
 mov ax,4c00h
 int 21h

I'm sory for my english, I'm form Poland.
I programed for TASM and I didn't have any problems with it.
This program is easy. It displays "hello, world" on output.

Now I begin programing for NASM and I have problem with this simply program.
I want to compiled 16-bits program for MS-DOS platform.
My compilation:
 nasm -f obj a.asm -o a.exe

Assembler don't display any errors or warrings, but program don't display "hello, word".
It display rabish :(
I don't know what happen. Could you help me. How I should compile this program?
 

Offline Keith Kanios

  • Full Member
  • **
  • Posts: 383
  • Country: us
    • Personal Homepage
Re: Problem with first program for NASM
« Reply #1 on: January 13, 2012, 01:05:15 AM »
You are trying to output an object file and run it as an executable. You have to link it first, or write it as a COM file (ORG 0x100) with NASM's -f bin format.

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: Problem with first program for NASM
« Reply #2 on: January 13, 2012, 02:00:51 AM »
The problem (I think) is that Nasm's "-f obj" produces a linkable object file, not a ready-to-run executable. Using "-o a.exe" will cause the file to be named .exe, but it isn't one! I'm surprised that it runs at all.

If you assemble it with:

Code: [Select]
nasm -f obj a.asm

... without the "-o" switch, Nasm will ("should") produce "a.obj" which will then need to be linked into an executable. If you've used Tasm, you've probably got Tlink, and probably know the proper command line. I think just:

Code: [Select]
tlink a.obj

would work, although you may want to put some (3?) commas after it to avoid having to hit "enter" to accept the defaults. Other linkers would work, too. Alink is good...

http://alink.sf.net

Be aware that the "usual" Microsoft "link" is for 32-bit code - you'll need the 16-bit version! If the banner says "incremental linker", that's the wrong version - you want "segmented linker".

It might be easier to make a .com file - Nasm will do this directly with no linker needed. You'll need some minor changes to your code...

Code: [Select]
; neither of these is necessary
; they both do the same thing
; won't hurt...
 use16
 bits 16 ;for 16 bits program

; this doesn't "cause" our program to be loaded at 100h
; dos does that with a .com file
; but we need to tell Nasm that this is so.
org 100h

; ".data", not "data" is a "known" segment name
; in "-f bin" output format
; this merely moves "segment .data" after ".text"
; which we need, as a .com file starts at the top
 segment .data

 hello: db 'hello, world','$'


; don't need this for "-f bin"
; stack will be at top of our one-and-only segment
; segment _stack stack
; resb 64
; stacktop:

; ".text" is the "known" segment name in "-f bin"
 segment .text

; only "-f obj knows "..start"
; a .com file will start at the beginning of ".text"
; ..start:

; don't need/want this for "-f bin"
; segment registers are set by dos in a .com file
; mov ax,data
; mov ds,ax

; if you've got the stack declared correctly
; which you do (did)
; you don't even need this in an .exe
; yeah, I know the example in the manual shows it
; but dos loads ss:sp for us
; mov ax,_stack
; mov ss,ax
; mov sp,stacktop

; from here, just the same.
 mov dx,hello
 mov ax,0900h
 int 21h
 mov ax,4c00h
 int 21h

Assemble that with:

Code: [Select]
nasm -f bin -o a.com a.asm

Note that you do need the "-o" switch in "-f bin" mode, or Nasm will name our gem just "a", which dos won't recognise. Could rename it to "a.com", too. Actually, we could name it "a.exe" and it would still work - dos looks for the "MZ" signature, and runs it as a .com file if it's not there...

The only disadvantage to a .com file is that you can't link multiple objects together, which you don't need for this, but might want later...

If you can't get it working with one or another of these methods, get back to us. As you say, it should be "easy"... (but sometimes the "easy" things aren't!).

Best,
Frank


Offline KrukersRadek

  • Jr. Member
  • *
  • Posts: 5
Re: Problem with first program for NASM
« Reply #3 on: January 13, 2012, 01:33:25 PM »
Thanks for your help very much!

Yesterday, at 2pm o'clock I resolved this problem the same way like you Frank Kotler.



Tasm and Tlink ruuulezzz! hahahaha

Greetings from Poland for you!