Author Topic: How do I properly set up nasm to be the output of a compiler?  (Read 11283 times)

Offline lord_raven

  • Jr. Member
  • *
  • Posts: 4
I am writing a compiler and I want the output to be in nasm. The tutorial I was learning from uses gas, and I want to convert it into nasm. I think the project would go further in nasm.  https://github.com/ravenleeblack/Willow

In code_gen.c, I am using fprintf functions, to print out nasm code into the file out.s, that can be used as the output to get assembled. I have been placing these functions, where I think they should be in the grammar. I know in normal circumstances I should be using an ast, but for now I am just manually doing it, to quickly test and get the language done step by step.

The issue I am having is setting nasm up itself. I haven't found many tutorials using nasm with compilers.

In my language, I have two kinds of functions or two classes of functions, one that acts like a class or module and the other is a normal function.

In this instance, Would I use a normal label for the first class function and a local label for the second class functions? I wanted to create a new scope called universal. This universal scope would be outside of any first class function, with global being inside the first class function, and local being inside second class functions. That is why my thought was that I might have to use a label with local labels.

 Is there a better way to approach this? Any tutorials I may have missed that would help answer some questions?

Code: [Select]

        .text
global _start

_start:
           push ebp
   mov ebp, esp
           sub esp, 32

   call  main
           call exit

main:

   mov   ebx, i
   mov   eax, 6
           mov         ebx, eax

   mov   ecx, nam
   mov   edx, 5
           mov         ecx , edx

   call    main.entry

.entry:
      mov    ebx, .entry
      push   ebx
      call main.hi
              ret

.hi:
      mov    ebx, .hi
      push   ebx
      ret

exit:
           mov    ebx, 0
   mov    ebx, 1
   int    80h

« Last Edit: June 29, 2022, 10:20:59 PM by lord_raven »

Offline debs3759

  • Global Moderator
  • Full Member
  • *****
  • Posts: 224
  • Country: gb
    • GPUZoo
Re: How do I properly set up nasm to be the output of a compiler?
« Reply #1 on: June 12, 2022, 06:19:02 PM »
You might find it useful to read the docs, which you can find at https://nasm.us/docs.php

The docs will tell you everything you can do with nasm, from basic instructions through to macros and more. You will need to work out (possibly by looking at examples, such as, for example, gcc source code) how to write the basic functions, such as io functions and others. A guide to Linux syscalls (I'm assuming you are writing for Linux, but I can't help with that as only generally write code for DOS or OS independent code) or BIOS interrupts (if writing for DOS or OS independent, that you will find at https://www.ctyme.com/rbrown.htm) will be helpful as well. If writing for Windows, someone else will have to direct you.
My graphics card database: www.gpuzoo.com

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: How do I properly set up nasm to be the output of a compiler?
« Reply #2 on: June 14, 2022, 03:14:57 AM »
Hi  lord_raven,

I haven't written a compiler, so I can't help you much.

Nasm doesn't really like your code. Throws a warning right on the first line. Not a good start, I'd say. We can configure Nasm to not emit the warning, but that's not what I think you should do. Your compiler ought to produce code that Nasm likes, I think. There are some things that Nasm doesn't complain about, but which I don't understand. Loading a register with a value and then loading the same register with a different value. You do this in a few places. I don't ;npw what your compiler intends... but then, I haven't written a compiler.

I wish you luck with your project. I hope you don't get too frustrated with it.

Best,
Frank

Offline Wilford81J

  • Jr. Member
  • *
  • Posts: 10
Re: How do I properly set up nasm to be the output of a compiler?
« Reply #3 on: February 07, 2023, 06:24:32 AM »
To properly set up NASM (Netwide Assembler) as the output of a compiler, you will need to perform the following steps:

Install NASM: If you don't already have NASM installed, you can download it from the official website (https://www.nasm.us/). Follow the installation instructions for your operating system.
MyCenturaHealth
Configure the Compiler: You will need to configure the compiler to output NASM-compatible assembly code. The exact steps for this will depend on the compiler you are using.

Assemble the Output: Once you have generated the NASM-compatible assembly code, you will need to use NASM to assemble it into an executable binary. You can do this by running the following command:

nasm -f format -o outputfile inputfile
Replace "format" with the desired format (e.g. "elf" for Linux or "macho" for MacOS), "outputfile" with the desired output filename, and "inputfile" with the input NASM-compatible assembly code file.

Link the Binary: In most cases, the assembled binary will still need to be linked with libraries to create a fully executable program. The exact steps for linking will depend on your operating system and the libraries you are using.
By following these steps, you can properly set up NASM as the output of your compiler and create executable programs using assembly code.
« Last Edit: February 08, 2023, 03:56:15 AM by Wilford81J »