3

I am just started learning Assembly programming. So far I know, NASM is the best assembler for linux. And its 32-bit assembler.

On the other hand, MASM is a DOS assembler and its 16 bit.

Now I want to know how to port from 16-bit x86 DOS assembly code to 32-bit x86 Linux assembly code or 32-bit x86 Windows assembly code

Thanks in Advance.

nrz
  • 10,435
  • 4
  • 39
  • 71
Md. Al-Amin
  • 1,423
  • 1
  • 13
  • 26
  • 5
    NASM is not "32-bit": [Chapter 8: Writing 16-bit Code (DOS, Windows 3/3.1)](http://www.nasm.us/doc/nasmdoc8.html). Also, MASM is not "16-bit": "Beginning with MASM 8.0 there are two versions of the assembler - one for 16-bit and 32-bit assembly sources" [wikipedia](http://en.wikipedia.org/wiki/Microsoft_Macro_Assembler) – Jongware Dec 16 '13 at 10:53
  • Your question is unclear. Whether the code is 16-bit/32-bit/64-bit is only one thing affecting the porting, the other is the **OS**. Do you mean how to port from 16-bit x86 **DOS** assembly code to 32-bit x86 **Linux** assembly code or 32-bit x86 **Windows** assembly code **or something else?** Please specify. – nrz Dec 16 '13 at 11:12
  • Yes, I want to know ow to port from 16-bit x86 DOS assembly code to 32-bit x86 Linux assembly code or 32-bit x86 Windows assembly code. – Md. Al-Amin Dec 16 '13 at 11:13
  • related: [Differences between NASM, MASM, emu8086, and other flavours of Intel syntax](https://stackoverflow.com/questions/44853636/how-to-know-if-an-assembly-code-has-particular-syntax-emu8086-nasm-tasm). – Peter Cordes Oct 02 '17 at 05:42

2 Answers2

6

MASM is not just 16 bit. You can also get 32 bit assember MASM:

http://masm32.com/

DOS assembly and Linux Assembly are completely different. There is no automated way to port. You will have to write separate code for each since assembly is most close to the metal.

Madhur Ahuja
  • 22,211
  • 14
  • 71
  • 124
  • 1
    "since the system call APIs are completely different" would be a more specific way to state that last point. If it was just different ABIs for the same API, the porting effort might be much smaller, and maybe some of it could be automated. Being close to the metal means that it's more work to port to a different API/ABI, of course, but a C program would need a major rewrite for Linux vs. DOS. – Peter Cordes Mar 18 '16 at 05:45
  • @Madhur, Do you recommend MASM or NASM? – Pacerier May 16 '17 at 18:45
4

NASM can handle x86 from 8bit to 64 bit, don't worry about it. NASM is a good all around assembler.

They both use Intel style assembly code, which is nicer in my opinion than AT&T style assembly code, you should do fine.

They are assemblers, as long as you understand the interrupts you call, they will accept your code, turn it into binary, and send it off merrily to be executed.

Oh yeah, to generate a dos executable place ORG 100h above the entry point you want, and that's why dos was good for assembly programmers.

Edit: This was only one of the reasons, the others were that dos was heavily interrupt based, so knowing where the arguments go and what they are allowed for relatively easy system calls.

PE format is what you want to look for, I think there's a bit more manual footwork to do in NASM to make it work properly on windows...

violet_white
  • 404
  • 2
  • 15
  • 2
    Linux 32bit syscalls can be made with `int 0x80`. (But calling into the userspace side of the `sysenter` code in the `vdso` is faster). All registers are preserved, except eax (return value). The ABI is just as "interrupt based" as DOS, if that's what you mean. 64bit is also similar: use `syscall`, which preserves all registers except `rcx` and `r11`, and the return value. Windows code OTOH should not use kernel system calls directly, but rather use the win32 API through function calls into DLLs. So yes, you're stuck with the function call ABI. – Peter Cordes Mar 18 '16 at 05:51