Skip to content

Instantly share code, notes, and snippets.

@cr1901
Created February 4, 2015 13:00
Show Gist options
  • Save cr1901/f309e77fd29c1b11c1d5 to your computer and use it in GitHub Desktop.
Save cr1901/f309e77fd29c1b11c1d5 to your computer and use it in GitHub Desktop.
NASM Map Unix Sections to DOS Segments
.SUFFIXES : .asm .obj
AS=nasm
ASPPFLAGS=-DAPI=MSDOS_API -DCPU_TARGET=8086
ASFLAGS=-fobj
LINK=wcl
LINKFLAGS=-zq -bcl=dos -fm
RM=rm
RMFLAGS=-rf
sec2seg.exe: sec2seg.obj
$(LINK) $(LINKFLAGS) -fe=sec2seg.exe sec2seg.obj
.asm.obj:
$(AS) $(ASPPFLAGS) $(ASFLAGS) -o$@ -l$*.lst $<
clean:
$(RM) $(RMFLAGS) *.exe *.obj *.map *.lst
CPU CPU_TARGET
;Macros to define sections- see http://www.siliconkit.com/rebecca/help/masmpguide/Appendix_E.htm
GROUP DGROUP _DATA _BSS
%macro .text 0-1.nolist
%if %0 == 0
segment _TEXT public align=2 class=CODE
%else
segment %1 public align=2 class=CODE
%endif
%endm
%define .code .text
%macro .data 0-1.nolist
%if %0 == 0
segment _DATA public align=2 class=DATA
%else
segment %1 public align=16 class=%1
%endif
%endm
%macro .const 0-1.nolist
%if %0 == 0
segment CONST public align=2 class=CONST
%else
segment %1 public align=16 class=%1
%endif
%endm
%macro .bss 0-1.nolist
%if %0 == 0
segment _BSS public align=2 class=BSS
%else
segment %1 public align=16 class=%1
%endif
%endm
%define .data? .bss
%macro .fardata 0-1.nolist
%if %0 == 0
segment FAR_DATA private align=16 class=FAR_DATA
%else
segment %1 private align=16 class=FAR_DATA
%endif
%endm
%macro .farbss 0-1.nolist
%if %0 == 0
segment FAR_BSS private align=16 class=FAR_BSS
%else
segment %1 private align=16 class=FAR_BSS
%endif
%endm
%define .fardata? .farbss
;Shut up OMF linkers by declaring a stack segment
%macro .stack 0
segment STACK stack align=16 class=STACK
%endm
.text
..start:
mov ax, 4c00h
int 21h
.data
dw 0
.bss
resw 1
.const
db 'Hello world!'
.data MY_DATA
dw 0
.bss MY_BSS
resw 1
.fardata
dw 0
.farbss
resw 1
.fardata MY_FARDATA
dw 0
.farbss MY_FARBSS
resb 1
;Cause unsuppressible warning, though safe to ignore.
;.data
;dw 1
.stack
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment