Nasm x64 code for windows - windows

I searched the internet rather, an example of a NASM x64 for windows, but I found just one, and, not work :(, just found for linux, code someone could show an example of how to create a Hello world NASM x64 windows
x64 assembly code for NASM linux
x64 Assembly code for NASM windows

A topic talking about that : https://openclassrooms.com/forum/sujet/nasm-win64-bug-hello-world.
Someone brings up a solution in NASM [Win64].
main.asm
extern GetStdHandle
extern WriteFile
extern Sleep
extern ExitProcess
%define STD_OUTPUT_HANDLE (-11)
section .data
hello_str db "Hello World", 13, 10
hello_size equ ($ - hello_str)
output_handle dq 0
section .text
global main
main:
;Aligne la pile
and rsp, 0xFFFF_FFFF_FFFF_FFF0
mov rbp, rsp
;Récupère l'output standart
mov ecx, STD_OUTPUT_HANDLE
call GetStdHandle
mov [output_handle], rax
;Affiche le message
;Shadow Space
sub rsp, 48
mov rcx, [output_handle]
mov rdx, hello_str
mov r8d, hello_size
mov r9, 0
mov QWORD [rsp + 32], 0
call WriteFile
;Détruit l'espace de pile alloué pour les paramètres de la fonction
add rsp, 48
;Pause de 1000 millisecondes pour donner le temps de voir
;Shadow Space
sub rsp, 0x20
mov ecx, 1000
call Sleep
;Quitte
xor rcx, rcx
call ExitProcess
make.bat
#echo off
nasm.exe -fwin64 main.asm -o Obj/main.o
gpp.exe -s -m64 Obj/main.o -o hello.exe C:\Windows\System32\kernel32.dll
pause
"Note that I link with g++, but I think you can adapt it to your linker."

Related

get string input in windows x64 assembly

bits 64
default rel
segment .data
grs db "name: "
inpt db "%s"
segment .text
global main
extern printf
extern scanf
extern _CRT_INIT
extern ExitProcess
global main
exit:
xor rax, rax
call ExitProcess
main:
push rbp
mov rbp,rsp
sub rsp, 32
lea rcx, [grs]
mov rax, rcx
call printf ;printing the grs variable
lea rcx, [inpt];the problem starts here
jmp exit
i am very new to assembly, I push %s into scanf so I type something on the console (like hello world) and press enter. Logically, the text I wrote needs to be saved somewhere, but I couldn't find how to do it.
I use these two commands to build
nasm -f win64 -o "main.obj" "main.asm"
link "C:\Users\xx\Desktop\assembly_x64\get_input\main.obj" /subsystem:console /entry:main /defaultlib:ucrt.lib /defaultlib:msvcrt.lib /defaultlib:legacy_stdio_definitions.lib /defaultlib:Kernel32.lib /nologo /incremental:no /opt:ref /out:"main.exe"

Segmentation fault when using printf in nasm 64bit [duplicate]

This question already has an answer here:
glibc scanf Segmentation faults when called from a function that doesn't align RSP
(1 answer)
Closed 1 year ago.
I'm trying to call printf in nasm 64 bit linux. But it outputs segmentation fault when i run it. I alligned the stack, add a 0 escape character to the end of the string. But it still output segfault.
The code:
section .data
_DATA1 db "aa", 0
section .text
global main
extern printf
main:
sub rsp, 16
lea r13, [_DATA1]
mov rdi, r13
call printf
add rsp, 16
mov rax, 0
ret
assemble and link with
nasm -f elf64 a.asm
gcc -no-pie a.o
Where did i do something wrong?
Ok, i got it.
It turns out i need to add after main :
push rbp
mov rbp, rsp
So the code looks like this:
section .data
_DATA1 db "aa", 0
section .text
global main
extern printf
main:
push rbp
mov rbp, rsp
sub rsp, 16
lea r13, [_DATA1]
mov rdi, r13
call printf
add rsp, 16
mov rax, 0
mov rsp, rbp
pop rbp
ret
I realise that this is also what gcc do

Segfault on NASM Hello World Mac OSX 64 bit

I'm trying to learn NASM. I compile and run the file but it segfaults. The (64 bit) code follows:
global _main
section .text
_main:
mov rax, 0x2000004 ; write
mov rdi, 1 ; stdout
mov rsi, msg
mov rdx, msg.len
syscall
mov rax, 0x2000001 ; exit
mov rdi, 0
syscall
section .data
msg: db "Hello, world!", 10
.len: equ $ - msg
I compile and link it as:
nasm -f macho64 hello.asm
ld hello.o -e _main -o hello -macosx_version_min 10.7

Moving contents from .data section to register in NASM [duplicate]

This question already has an answer here:
NASM issue on OSX 64-bit [duplicate]
(1 answer)
Closed 4 years ago.
I'm new to NASM and have struggle moving contents of variable from .data section to register. Following code outputs "Value: 0" instead of "Value: 1". If I write constant to register directly (mov qword rax, 25) everything works OK.
; /usr/local/bin/nasm -f macho64 sum.asm && ld -macosx_version_min 10.7.0 -lSystem -o sum sum.o && ./sum
section .data
myvar: dq 1234
message: db "Value: %i", 10, 0
.len: equ $ - message
global start
extern _printf
extern _exit
section .text
start:
default rel
; This outputs "Value: 0"
mov qword [myvar], 1
mov rax, [myvar]
; This works:
; mov qword rax, 25
; Output
mov rsi, rax
mov qword rax, 0
lea rdi, [rel message]
call _printf
mov qword rax, 0
call _exit
/usr/local/bin/nasm -v says:
NASM version 2.11.08 compiled on Mar 10 2015
The OS X NASM 2.11.08 bug strikes again. Use an older version (like 2.11.06), or a newer version with a fix for relative symbol addressing in the data section. Or use yasm.
Like I said in comments, you can zero a 64bit register with xor eax, eax. That's the standard idiom.
Writing to a 32bit reg always clears the upper32 of the 64bit register. This saves a lot of instruction bytes compared to moving a 64bit immediate.
mov qword rax, 25
Is still a 32bit immediate move. The qword is unnecessary. The instruction does have an unneeded REX prefix to make it a 64bit write, instead of just automatically clearing the high 32 by writing the low 32.
mov eax, 25
does the same thing, but with fewer instruction bytes.
Your code is correct, except for the global entry point main which is needed (but not required, you can adjust the entry point -- with link options). Here you are linking with the libc printf and exit functions. While compilers differ, using printf rather than _printf can help.
With only those semantic changes (and compiling on Linux instead of Mac), your code gives the desired output:
section .data
myvar: dq 1234
message: db "Value: %i", 10, 0
.len: equ $ - message
global main
extern printf
extern exit
section .text
main:
default rel
; This outputs "Value: 0"
mov qword [myvar], 1
mov rax, [myvar]
; Output
mov rsi, rax
mov qword rax, 0
lea rdi, [rel message]
call printf
mov qword rax, 0
call exit
Compile
$ nasm -felf64 -o obj/label64.o label64.asm
$ ld -dynamic-linker /lib64/ld-linux-x86-64.so.2 /usr/lib64/crt1.o \
/usr/lib64/crti.o obj/label64.o /usr/lib64/crtn.o -lc -o bin/label64
Output
$ ./bin/label64
Value: 1
Note: you may need to adjust the paths needed by the link command. Otherwise, you can just call gcc and let it sort the paths out. E.g.:
$ gcc -o bin/label64 obj/label64.o

Illegal instruction: 4 (Mac 64-bit, NASM)

I'm trying to write a simple helloworld in assembler 64 on Mac with NASM.
Every time I try to run it I'm getting this error:
Illegal instruction: 4
Here is my code:
section .text
global _main
_main:
mov rax, 4
mov rbx, 1
mov rcx, tekst
mov rdx, dlugosc
int 80h
mov rax, 1
int 80h
section .data
tekst db "Hello, world", 0ah
dlugosc equ $ - tekst
I'm compiling with:
nasm -f macho64 HelloWorld.asm
And I'm linking with:
ld -o HelloWorld -arch x86_64 -macosx_version_min 10.10 -lSystem -no_pie HelloWorld.o
Any help is highly appreciated.
Let's start with the most important thing:
On Mac OSX, system calls are preceded by an 0x2000###, so for an exit it would 0x2000001.
Next, we need to use the correct registers to pass arguments.
The number of the syscall has to be passed in register rax.
rdi - used to pass 1st argument to functions
rsi - used to pass 2nd argument to functions
rdx - used to pass 3rd argument to functions
rcx - used to pass 4th argument to functions
r8 - used to pass 5th argument to functions
r9 - used to pass 6th argument to functions
A system-call is done via the syscall instruction. The kernel destroys registers rcx and r11.
So bringing this together, a fixed version of your code is:
section .text
global _main
_main:
mov rax, 0x2000004
mov rdi, 1
mov rsi, tekst
mov rdx, dlugosc
syscall
mov rax, 0x2000001
syscall
section .data
tekst db "Hello, world", 0ah
dlugosc equ $ - tekst

Resources