Author Topic: NASM, segments and procedures  (Read 15724 times)

Offline cprieto

  • New Member
  • Posts: 1
NASM, segments and procedures
« on: April 24, 2021, 12:52:39 PM »
I had been trying to replicate an example from FASM (flat assembler) to NASM but I had not been able to do it and I try to understand the way NASM handles segments (sections).

Code: [Select]
ormat mz
entry main:start
stack 100h

segment main
start:
  mov ax, text
  mov ds, ax

  mov dx, hello
  call extra:write_text

  mov ax, 4c00h
  int 21h

segment text
  hello db 'Hello world!', 24h

segment extra
write_text:
  mov ah, 9
  int 21h
  retf

FASM generates a MSDOS exe file and loading this in DEBUG I see the following:

Code: [Select]
0D9D:0000 B89F0D            MOV     AX,0D9F
0D9D:0003 8ED8              MOV     DS,AX
0D9D:0005 BA0000            MOV     DX,0000
0D9D:0008 9A0000A00D        CALL    0DA0:0000
0D9D:000D B8004C            MOV     AX,4C00
0D9D:0010 CD21              INT     21

As you may see, FASM creates the segment and set the CALL as a far call. extra is effectively placed in a separate segment. The data segment is in a separate segment as well.

I tried doing this example in NASM

Code: [Select]
section code
..start:
  mov ax, seg hello
  mov ds, ax

  mov dx, hello
  call extra:write_text

  mov ax, 4c00h
  int 21h

section data
  hello db "Hello World!", 24h

section extra
write_text:
  mov ah, 9
  int 21h
  retf

section stack class=stack
  resb 100h

And linking with wlink using this link file:

Code: [Select]
system  dos
option  map
name    seg2.exe
file    seg2.obj
[code]

The generated code is [i]slightly[/i] different:

[code]
0D9D:0000 B89D0D            MOV     AX,0D9D
0D9D:0003 8ED8              MOV     DS,AX
0D9D:0005 BA1200            MOV     DX,0012
0D9D:0008 9A1F009D0D        CALL    0D9D:001F
0D9D:000D B8004C            MOV     AX,4C00
0D9D:0010 CD21              INT     21
0D9D:0012 48                DEC     AX
0D9D:0013 65                GS:     (unused)
0D9D:0014 6C                INSB
0D9D:0015 6C                INSB
0D9D:0016 6F                OUTSW
0D9D:0017 20576F            AND     [BX+6F],DL
0D9D:001A 726C              JB      0088
0D9D:001C 642124            AND     FS:[SI],SP
0D9D:001F B409              MOV     AH,09

As you may see, write_text is in the same segment as the rest of the code (and apparently the data segment as well) but it was not put in a separate segment as I thought.

Am I missing something here? Is there any way to actually place code in a separate segment? Any help, guidance or explanation of what is happening or how to achieve such would be appreciated!

and yes, I tried in MASM and the same thing happens, is FASM the only one with that super power?

Thanks!

Offline tysonprogrammer

  • Jr. Member
  • *
  • Posts: 22
  • Country: us
  • C# application developer attempting assembly.
Re: NASM, segments and procedures
« Reply #1 on: January 11, 2022, 12:49:15 AM »
I am wondering if you solved this?

If not why not use

Code: [Select]
call seg write_text:write_text
That works for me.