Author Topic: Use nasm with Windows 7  (Read 12858 times)

Offline serge1

  • New Member
  • Posts: 1
Use nasm with Windows 7
« on: February 22, 2016, 10:04:14 AM »
Hello ,

I'm a beginner in assemble language and I wanted to learn it.

For this I found a good tutorial in assembly language. This tutorial is using nasm and is easier to follow in you have a linux computer.

However my computer has Windows 7.

I tried to use the hello world example of the tutorial. It compiles and it links but when executing it, it crashes. Therefore either the code itself needs to be slightly modified for windows or the compile or link instructions I used are not the right one.
Can anyone help me ?
You'll find below the code , compile and link instructions

Thank you in advance

Serge

Code: [Select]
section .text
 ;   global _start   ;must be declared for linker (ld)
    global_start     ;must be declared for linker (ld)
_start:             ;tells linker entry point
    mov edx,len     ;message length
    mov ecx,msg     ;message to write
    mov ebx,1       ;file descriptor (stdout)
    mov eax,4       ;system call number (sys_write)
    int 0x80        ;call kernel

    mov eax,1       ;system call number (sys_exit)
    int 0x80        ;call kernel

section .data
msg db 'Hello, world!', 0xa  ;our dear string
len equ $ - msg     ;length of our dear string

Compile instructions:

nasm -f win32 test.asm -o test.o

Link instructions:

ld test.o -o test.exe

« Last Edit: February 22, 2016, 05:41:51 PM by Frank Kotler »

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: Use nasm with Windows 7
« Reply #1 on: February 22, 2016, 06:53:53 PM »
Hi serge1,

Welcome to the Forum! I edited your post to put "code tags" around your code. Just the word "code" in square brackets at the beginning of your code amd "/code" in square brackets at the end. This supposedly makes your code easier to read, and definitely makes it easier to cut-and-paste.

Don't worry about that too much, because it's the "wrong code" anyway. That code is for Linux and won't work for Windows. Despite the fact that Linux is a less "popular" OS, a lot of people get ahold of that example - the "dear string" comment identifies it as a very old example. I don't know why Windows programmers find it, but you're not the first one it has happened to!

Your command line to Nasm is good, and your command line to ld may be okay. Golink is more popular, but ld should also work fine. You will need different code! I haven't run Windows since win98 was in use, so I'm badly out-of-date. I hope we can get a current Windows user in here to help you. Meanwhile...

There is some valid Windows code here:

http://forum.nasm.us/index.php?topic=1918.0

Code more like what you've got for Linux - something that will write to the console - would involve "WriteFile"... and you'll need to call "GetStdHandle" to find out what STDOUT is (it's always 1 for Linux). I strongly suggest you end with "ExitProcess" rather than just "ret", although it may work.

This is untested code, and almost certainly has errors, but the code you want will look "something like this"...
Code: [Select]
; nasm -f win32 myprog.asm
; ld -o myprog.exe myprog.obj
; might be myprog.o and you might need a .lib file

extern GetStdHandle
extern WriteFile
extern ExitProcess

section .data
    msg db "hello windows!", 0
    msglen equ $ - msg ; note: a constant not a variable
section .bss
; Linux returns this in eax
; Windows needs a "place to put it"
    bytes_written resd 1
section .text

global start ; or perhaps winmain
start:
; find out what STDOUT is
    push -11
    call GetStdHandle
; now the handle for STDOUT is in eax
    push 0
    push bytes_written
    push msglen
    push msg
    push eax
    call WriteFile

    push 0
    call ExitProcess
;----------------------------
One of the things that could be wrong with that is the "true names" of the APIs. It might be "_GetStdHandle@4", "_WriteFile@20" and "_exitprocess@4". Maybe "_winmain@16", too? The number after the '@' is the number of bytes of parameters (not the number of parameters). I think the linker you use (or the library?) "simplifies" the names for you and some don't.

I hope a Windows user will provide you with some tested code, but give it a shot. You'll never learn assembly language if you give up easy. :) Good luck!

Best,
Frank