Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9236799
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T07:23:05+00:00 2026-06-18T07:23:05+00:00

I wrote a program in Assembly (x86-64) and called it from a C file

  • 0

I wrote a program in Assembly (x86-64) and called it from a C file wrapper to use both the printf and scanf functions. However when I try to link it I get the following errors:

$ nasm -f macho64 -l Parallelograms.lis -o assembly.o Parallelograms.asm
$ gcc -c -Wall -m64 -o main.o ParallelogramDriver.c
$ gcc -m64 -o main.out main.o assembly.o
Undefined symbols for architecture x86_64:
  "_calcAndPrint", referenced from:
      _main in main.o
  "printf", referenced from:
      calcAndPrint in assembly.o
  "scanf", referenced from:
      calcAndPrint in assembly.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status

I believe it’s related to the macho64 flag. If I were to use elf64 it wouldn’t catch the printf or scanf error. However elf64 is an incompatible file format on Macs (as I understand it).

Here’s my C file (ParallelogramDriver.c):

#include <stdio.h>
#include <stdint.h> //For C99 compatability

extern int calcAndPrint();

int main(int argc, char* argv[])
{
    //Run Assembly Code
    calcAndPrint();

    return 0;
}

And finally here’s my Assembly code (Parallelograms.asm):

;==== Begin code area ====
extern printf                                       ;External C function for output
extern scanf                                        ;External C function for input
extern sin                                          ;External C function for sin math function

segment .data                                       ;Place initialized data in this segment

    stringData db "%s", 10, 0
    input1 db "Enter the length of one side of the parallelogram: ", 0
    input2 db "Enter the length of the second side of the parallelogram: ", 0
    input3 db "Enter the size in degrees of the included angle: ", 0

    floatOutput db "You entered: %5.2Lf", 0         ;Don't forget the uppercase L

    floatData db "%Lf", 0

segment .bss                                        ;Place uninitialized data in this segment

    ;Currently this section is empty

segment .text                                       ;Place executable statements in this segment

    global calcAndPrint

calcAndPrint:                                       ;Entry Point Label.

;==== Necessary Operations! Do not remove!
    push        rbp                                 ;Save a copy of the stack base pointer !IMPORTANT
    push        rdi                                 ;Save since we will use this for our external printf function
    push        rsi                                 ;Save since we will use this for our external printf function

;==== Enable Floating Point Operations
    finit                                           ;Reset pointers to st registers; reset control word, status word, and tag word.

;============ INPUT 1 ==============
;==== Ask for first input
    mov qword   rax, 0                              ;A zero in rax indicates that printf receives standard parameters
    mov         rdi, stringData
    mov         rsi, input1
    call        printf

;==== Grab input from Keyboard
    mov qword  rax, 0                               ;A zero in rax indicates that printf receives standard parameters
    mov        rdi, floatData                       ;Tell scanf to accept a long float as the data input
    push qword 0                                    ;8 byes reserved. Need 10 bytes
    push qword 0                                    ;Another 8 bytes reserved for a total of 16 bytes
    mov        rsi, rsp                             ;rsi now points to the 16 bytes we have open. (rsp = Stack Pointer)
    call       scanf                                ;C now uses the scanf function

;==== Copy 10 byte number into Float space
    fld tword  [rsp]                                ;Load Float space and push rsp into the float stack. (braquests de-reference)

;============ INPUT 2 ==============
;=== Ask for second input
    mov qword   rax, 0                              ;A zero in rax indicates that printf receives standard parameters
    mov         rdi, stringData
    mov         rsi, input2
    call        printf

;==== Grab input from Keyboard
    mov qword  rax, 0                               ;A zero in rax indicates that printf receives standard parameters
    mov        rdi, floatData                       ;Tell scanf to accept a long float as the data input
    push qword 0                                    ;8 byes reserved. Need 10 bytes
    push qword 0                                    ;Another 8 bytes reserved for a total of 16 bytes
    mov        rsi, rsp                             ;rsi now points to the 16 bytes we have open. (rsp = Stack Pointer)
    call       scanf                                ;C now uses the scanf function

;==== Copy 10 byte number into Float space
    fld tword  [rsp]                                ;Load Float space and push rsp into the float stack. (braquests de-reference)

;============ INPUT 3 ==============
;=== Ask for third input
    mov qword   rax, 0                              ;A zero in rax indicates that printf receives standard parameters
    mov         rdi, stringData
    mov         rsi, input3
    call        printf

;==== Grab input from Keyboard
    mov qword   rax, 0                              ;A zero in rax indicates that printf receives standard parameters
    mov         rdi, floatData                      ;Tell scanf to accept a long float as the data input
    push qword  0                                   ;8 byes reserved. Need 10 bytes
    push qword  0                                   ;Another 8 bytes reserved for a total of 16 bytes
    mov         rsi, rsp                            ;rsi now points to the 16 bytes we have open. (rsp = Stack Pointer)
    call        scanf                               ;C now uses the scanf function

;==== Copy 10 byte number into Float space
    fld tword  [rsp]                                ;Load Float space and push rsp into the float stack. (braquests de-reference)

;============ TEMP ==============

;============ Output ==============
    mov qword   rax, 0
    mov         rdi, floatOutput
    mov qword   rax, 1                              ;Important for floats??!
    push qword  0                                   ;8 bytes reserved
    push qword  0                                   ;16 bytes reserved
    fstp tword [rsp]                                ;Pop the fp number from the FP stack into the storage at [rsp]
    call       printf

;============ Restore Registers ============
    pop rsi
    pop rdi
    pop rbp                                         ;Restore base pointer

;==== Time to exit this function ====
;Prepare to exit from this function
    mov qword rax, 0                                ;A zero in rax is the code indicating a successful execution.
    ret                                             ;ret pops the stack taking away 8 bytes

;==== End of function calcAndPrint ====

Apologies for the messy code. This is my first Assembly code program and I’m very new to it. I’m developing on Mac OSX and to my knowledge this is a Mac OSX specific issue. Thanks for your help.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T07:23:06+00:00Added an answer on June 18, 2026 at 7:23 am

    On OS X, the symbols are prefixed with an underscore.

    call        printf
    

    and

    call        scanf
    

    need to be

    call        _printf
    

    and

    call        _scanf
    

    respectively; also

    global calcAndPrint
    

    and

    calcAndPrint:
    

    should read

    global _calcAndPrint
    

    and

    _calcAndPrint:
    

    instead.

    (But hey, you could have deduced this from the fact that your calcAndPrint() function was symbolicated into _calcAndPrint).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wrote a program called Hello.py that looks like this: import pygame, sys from
I wrote a simple program in x86 assembly and I tried to run it
I am trying to convert an assembly program I wrote into NULL-free shellcode. However,
I wrote a x86 assembly program for MBR section. I compile it as follows:
I wrote a program for downloading an image from web using AsyncTask in service
I wrote a .net assembly using c# to perform functions that will be used
I have a big problem with a program in assembly x86. I have to
I wrote a simple program in assembly and I tried to run it using
I have just started to teach myself x86 assembly on linux from these video
So I am working on an x86 Assembly program for Linux using NASM. This

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.