21
submitted 1 day ago* (last edited 1 day ago) by Tomorrow_Farewell@hexbear.net to c/technology@hexbear.net

I am trying to re-learn assembly. I have been trying to find a tutorial for assembling a program using NASM on Windows, on a CPU with the x86_64 architecture. I have been unable to make any of the provided examples work.

I am asking to be provided:

  • A piece of code to assemble. The resulting program should output a message into the CLI.
  • CLI commands to make an object file and to do linkage of that into an executable file.

This should preferably be done using NASM, on Windows, on x86_64 architecture, but I'm at my wit's end at this point, so I guess I will be fine with another assembler.

I intend to analyze the example and to use this as a starting point in my process of getting back into assembly.

top 11 comments
sorted by: hot top controversial new old
[-] Speaker@hexbear.net 3 points 1 day ago
[-] Tomorrow_Farewell@hexbear.net 1 points 12 hours ago

Sadly, this doesn't work. When trying to link the object file, I get a bunch of errors:

i386 architecture of input file `test.obj' is incompatible with i386:x86-64 output
test.obj:test.asm:(.text+0x6): undefined reference to `_printf'
undefined reference to `WinMain'
collect2.exe: error: ld returned 1 exit status

I currently do not have the time to debug this.

[-] footfaults@hexbear.net 6 points 1 day ago
[-] Tomorrow_Farewell@hexbear.net 1 points 11 hours ago

Yep, this works.

[-] Tomorrow_Farewell@hexbear.net 3 points 1 day ago

Thank you.

Will look into it when I have time (probably not today, unfortunately).

[-] Shinji_Ikari@hexbear.net 4 points 1 day ago

For the CLI commands: https://www.nasm.us/xdoc/2.16.03/html/nasmdoc2.html#section-2.1

Have you seen this?

I haven't written asm in years though, so I cant whip up a putc loop example.

[-] Tomorrow_Farewell@hexbear.net 2 points 1 day ago

No, but this doesn't provide any relevant answers.

[-] Mardoniush@hexbear.net 5 points 1 day ago

It's probably way too introductory for you, but the game Turing Complete gets you to make your own toy assembly language (twice for different architectures) in the later parts and this helped me immensely when learning assembly myself.

[-] Tomorrow_Farewell@hexbear.net 4 points 1 day ago

While curious, that's not at all what I'm looking for in this case. I am looking for an example of working code and commands to build an executable from it.

[-] lilypad@hexbear.net 5 points 1 day ago* (last edited 1 day ago)

Its not windows or NASM but this site has some 64 bit linux examples using gnu assembler (the gnu userland default assembler). You could probably find some examples for windows with nasm if you look around.

::: spoiler example code from the site

# ----------------------------------------------------------------------------------------
# Writes "Hello, World" to the console using only system calls. Runs on 64-bit Linux only.
# To assemble and run:
#
#     gcc -c hello.s && ld hello.o && ./a.out
#
# or
#
#     gcc -nostdlib hello.s && ./a.out
# ----------------------------------------------------------------------------------------

        .global _start

        .text
_start:
        # write(1, message, 13)
        mov     $1, %rax                # system call 1 is write
        mov     $1, %rdi                # file handle 1 is stdout
        mov     $message, %rsi          # address of string to output
        mov     $13, %rdx               # number of bytes
        syscall                         # invoke operating system to do the write

        # exit(0)
        mov     $60, %rax               # system call 60 is exit
        xor     %rdi, %rdi              # we want return code 0
        syscall                         # invoke operating system to exit
message:
        .ascii  "Hello, world\n"
[-] Tomorrow_Farewell@hexbear.net 4 points 1 day ago

Unfortunately, I am looking specifically for examples for Windows. I need to figure out what I'm doing wrong.

You could probably find some examples for windows with nasm if you look around

I have looked around, and I have not found any working examples of such. That is exactly why I'm asking.

this post was submitted on 20 Oct 2024
21 points (100.0% liked)

technology

23239 readers
86 users here now

On the road to fully automated luxury gay space communism.

Spreading Linux propaganda since 2020

Rules:

founded 4 years ago
MODERATORS