Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to call 6502 assembly code from C file?

Tags:

c

6502

I am using cc65 6502 simulator, which compiles code for 6502. I wish to link the 6502 code and C code and produce a binary file that I can execute.

My C code "main.c":

 #include<stdio.h>
 extern void foo(void);

 int main() {
    foo();
    return 0;
 }

My 6502 code "foo.s":

 foo:
      LDA #$00
      STA $0200

The code might seem very simple but I am just trying to achieve the successful linking. But I cannot get rid of the following error:

Unresolved external '_foo' referenced in: main.s(27) ld65: Error: 1 unresolved external(s) found - cannot create output file

like image 233
Milap Jhumkhawala Avatar asked Sep 03 '18 01:09

Milap Jhumkhawala


People also ask

Can you run assembly in C?

We can write assembly program code inside c language program. In such case, all the assembly code must be placed inside asm{} block. Let's see a simple assembly program code to add two numbers in c program.

What is a 6502 assembler?

6502 assembly is a very low-level language that works specifically for the 6502 microprocessor — a very popular processor from the 1970s.


1 Answers

You need to export it from the assembly module - with the same decoration the C compiler uses:

_foo:
.export _foo
      LDA #$00
      STA $0200

This links with:

cl65 -t sim6502 main.c foo.s -o foo

You might also need to look into the calling conventions.

like image 94
Nick Westgate Avatar answered Nov 15 '22 05:11

Nick Westgate