masm to nasm assembly conversion example

Here’s a simple example demonstrating some of the major syntax differences between Microsoft’s assembler and the free nasm assembler. It mainly implements the suggestions here.

TITLE MASM Example (main.asm)

INCLUDE Irvine32.inc

newLine = 10

printVal MACRO str, val
push eax
push edx
mov edx, OFFSET str
call WriteString
mov eax, val
call WriteInt
sub esp, 4
mov DWORD PTR [esp], newLine
pop eax
call WriteChar
pop edx
pop eax
ENDM

.data
outStr BYTE “The answer is “,0
answer DWORD ?

.code

printAnswer PROC
printVal outStr, answer
ret
printAnswer ENDP

main PROC
mov answer, 42

call PrintAnswer

exit
main ENDP

END main

————————————————

; TITLE NASM Example (main.asm)

%include “Along32.inc”

newLine EQU 10

%macro printVal 2
push eax
push edx
mov edx, %1
call WriteString
mov eax, [%2]
call WriteInt
sub esp, 4
mov DWORD [esp], newLine
pop eax
call WriteChar
pop edx
pop eax
%endmacro

SECTION .data
outStr: db “The answer is “,0
answer: resd 1

SECTION .text

printAnswer:
printVal outStr, answer
ret

global main

main:
mov DWORD [answer], 42

call printAnswer

ret

Comments are closed.