Author Topic: NASM tutorial for Windows (7+), console and windows  (Read 34288 times)

Offline kanito73

  • Jr. Member
  • *
  • Posts: 10
NASM tutorial for Windows (7+), console and windows
« on: November 17, 2018, 02:10:48 AM »
Hello everybody

I'm new to NASM and need a good tutorial to start programming on Windows 7. I've just installed NASM for Win64 and the linker GoLink (mentioned in a few forums) but was unsuccessful my first try due the lack of documentation for Windows (99% available is for Linux), I was able to assemble and link a minimalist (do nothing) example just to test, but when I open the EXEcutable with a debugger (x64dbg) the disassembled code is weird and nothing of my code shows there, as if it was just garbage code. Many people uses NASM on Windows, so it should work and by sure the problem it's me.

I would be so grateful if you share a good tutorial or at least an example of a very minimalist program (mov ax,0... and exit, just that) and how to assemble and generate the EXE file IN WINDOWS with a recommended linker.

I already program ASM in DOS using Turbo Assembler and (my favorite) A86 assembler, in fact I choosed NASM due the (relatively) similarity (the concept) with A86, but generating executables now for Windows (first for console, later for windows app)...

The steps I followed:

nasm -f win32 mytest.asm

golink mytest.obj

but did not work...


Thanks for any help, idea or documentation...


P.S. I want to make standalone programs (by now), don't want to use (or interface with) Visual Studio or other compilers.
« Last Edit: November 17, 2018, 02:16:39 AM by kanito73 »

Offline dreamCoder

  • Full Member
  • **
  • Posts: 107
Re: NASM tutorial for Windows (7+), console and windows
« Reply #1 on: November 17, 2018, 11:57:30 AM »
Here's one simple example for you to begin with;
Code: [Select]
global start

extern printf        ;from msvcrt
extern scanf         ;from msvcrt
extern ExitProcess   ;from kernel32

section .bss
name:   resb 100

section .data
prompt: db 'Enter your name: ',0
frmt:   db '%s',0
greet:  db 'Hello, %s!',0ah,0

section .text
start:
        sub     rsp,40

        mov     rcx,prompt
        call    printf

        mov     rdx,name
        mov     rcx,frmt
        call    scanf

        mov     rdx,name
        mov     rcx,greet
        call    printf

        xor     ecx,ecx
        call    ExitProcess

;nasm -f win64 myprog.asm
;golink /console myprog.obj msvcrt.dll kernel32.dll

Try that out

Offline dreamCoder

  • Full Member
  • **
  • Posts: 107
Re: NASM tutorial for Windows (7+), console and windows
« Reply #2 on: November 17, 2018, 12:04:32 PM »
And here's the Win64 Windows version of MessageBox
Code: [Select]
global start

NULL  equ 0
MB_OK equ 0

extern MessageBoxA   ;from user32
extern ExitProcess   ;from kernel32

section .data
hello:  db 'Hello, Windows!',0
title:  db 'My First Win64',0

section .text
start:
        sub     rsp,40

        mov     r9,MB_OK
        mov     r8,title
        mov     rdx,hello
        mov     rcx,NULL
        call    MessageBoxA

        xor     ecx,ecx
        call    ExitProcess

;nasm -f win64 myprog.asm
;golink myprog.obj user32.dll kernel32.dll

NASM is easy-going :D


Offline dreamCoder

  • Full Member
  • **
  • Posts: 107
Re: NASM tutorial for Windows (7+), console and windows
« Reply #3 on: November 17, 2018, 12:09:35 PM »
MessageBox, 32-bit version
Code: [Select]
global start

NULL  equ 0
MB_OK equ 0

extern MessageBoxA   ;from user32
extern ExitProcess   ;from kernel32

section .data
hello:  db 'Hello, Windows!',0
title:  db 'My First Win32',0

section .text
start:

        push    MB_OK
        push    title
        push    hello
        push    NULL
        call    MessageBoxA

        push    0
        call    ExitProcess

;nasm -f win myprog.asm
;golink myprog.obj user32.dll kernel32.dll

Offline dreamCoder

  • Full Member
  • **
  • Posts: 107
Re: NASM tutorial for Windows (7+), console and windows
« Reply #4 on: November 17, 2018, 12:12:38 PM »
32-bit command-line prompt greeting
Code: [Select]
global start

extern printf        ;from msvcrt
extern scanf         ;from msvcrt
extern ExitProcess   ;from kernel32

section .bss
name:   resb 100

section .data
prompt: db 'Enter your name: ',0
frmt:   db '%s',0
greet:  db 'Hello, %s!',0ah,0

section .text
start:
        push    prompt
        call    printf
        add     esp,4

        push    name
        push    frmt
        call    scanf
        add     esp,8

        push    name
        push    greet
        call    printf
        add     esp,8

        push    0
        call    ExitProcess

;nasm -f win myprog.asm
;golink /console myprog.obj msvcrt.dll kernel32.dll

Have a pleasant journey  ;D
« Last Edit: November 19, 2018, 12:05:20 PM by dreamCoder »

Offline kanito73

  • Jr. Member
  • *
  • Posts: 10
Re: NASM tutorial for Windows (7+), console and windows
« Reply #5 on: November 17, 2018, 04:43:44 PM »
Thanks all for your time and examples, it will be very helpful for me. Just needed a start point...

Offline nasmer

  • New Member
  • Posts: 1
Re: NASM tutorial for Windows (7+), console and windows
« Reply #6 on: September 19, 2021, 08:55:33 PM »
Thanks alot for your effort from me too.

Offline JohnG

  • Jr. Member
  • *
  • Posts: 33
Re: NASM tutorial for Windows (7+), console and windows
« Reply #7 on: January 06, 2022, 10:40:56 AM »
Hi all,

The examples here helped a lot. Mainly about what was needed at the command line to compile with things like printf and scanf.

John

Offline dreamCoder

  • Full Member
  • **
  • Posts: 107
Re: NASM tutorial for Windows (7+), console and windows
« Reply #8 on: February 25, 2022, 10:40:21 PM »
Whewww... it has been years past. Glad my simple examples here have helped people getting started painlessly. Havent been programming quite some time but for beginners looking for more advanced "helper" library, I recommend using  https://sourceforge.net/projects/baselibs/files/. A very lightweight library offering debugger-like features. Support both Win and Linux, too.