flat assembler
Message board for the users of flat assembler.

Index > Linux > Generating object file to link it with gcc

Author
Thread Post new topic Reply to topic
vanquish



Joined: 25 Feb 2018
Posts: 3
vanquish 25 Feb 2018, 08:21
I am new to assembly language, I would like to pass values from C program to Assembly using the linked assembly method instead of inline assembly method in C. Below is the assembly program which I am working on
Code:
format ELF64 executable 3

segment readable executable

entry bitgamesasm

bitgamesasm:
    push rbp 
    mov rbp, rsp 
    
    mov rbx, rdi        ;load rbx with a
    mov rcx, rsi        ;load rcx with b
    xor rdx, rdx        ;initialize rdx(result) 0

countBits:
    ror rbx, 1            ;rotate right
    jnc bitIsZero       ;if C flag not set do not increment 
    inc rdx               ;if C flag set increment rdx

bitIsZero:
    loop countBits              

exit:
    mov rax, rdx        ; return value store in rax
    mov rsp, rbp 
    pop rbp 
    ret     


And this is the C program from with I am trying to pass the values to assembly program

Code:
#include<stdio.h>

extern unsigned short bitgamesasm(int a, int b); 

int main(void){
    
    unsigned int a = 0;
    unsigned short b = 0;
    unsigned short result = 0;

    printf("Enter the number to check number of set bits: ");
    scanf("%d",&a);

    printf("Enter number of bits to check 32 or 64 : ");
    scanf("%hu", &b);

    result = bitgamesasm(a, b); 

    printf("For the number %d, number of set bits are %hu\n", a, result);
    return 0;
}
    


Below is the way I assemble and compile the program
Code:
$ fasm bitgamesasm.fasm bitgamesasm.o
flat assembler  version 1.71.59  (16384 kilobytes memory)
2 passes, 151 bytes.

$ gcc bitgames.c bitgamesasm.o -o bitgames
/tmp/cckEcarp.o: In function `main':
bitgames.cSad.text+0x7c): undefined reference to `bitgamesasm'
collect2: error: ld returned 1 exit status    


I am using fasm 1.71.59 in MX17 Linux OS.
I am unable to figure out the error. Kindly help me out. Thank you.
Post 25 Feb 2018, 08:21
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20451
Location: In your JS exploiting you and your system
revolution 25 Feb 2018, 08:30
To create a linkable object files the format needs to be non-executable (i.e. just ordinary ELF). And you have to declare external symbols (extrn) for your asm code to call, and declare exported symbols (public) for your C code to call.
Post 25 Feb 2018, 08:30
View user's profile Send private message Visit poster's website Reply with quote
vanquish



Joined: 25 Feb 2018
Posts: 3
vanquish 25 Feb 2018, 08:59
@revolution Thank You very much. I did delete executable 3 and segment readable executable And add public keyword instead of entry I was able to assemble and run the program.

May I know why we should delete segment readable as well. I understood that `executable`needs to be removed since its going to be an object file, not an executable file.
Post 25 Feb 2018, 08:59
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20451
Location: In your JS exploiting you and your system
revolution 25 Feb 2018, 09:34
I'm not sure about the segment, but I suspect that segments are for executable files only. For linkable object you would use section.


Last edited by revolution on 25 Feb 2018, 23:06; edited 1 time in total
Post 25 Feb 2018, 09:34
View user's profile Send private message Visit poster's website Reply with quote
redsock



Joined: 09 Oct 2009
Posts: 435
Location: Australia
redsock 25 Feb 2018, 20:26
I did a fairly extensive writeup on C/C++ linking with my library that may be of help: https://2ton.com.au/rants_and_musings/gcc_integration.html

_________________
2 Ton Digital - https://2ton.com.au/
Post 25 Feb 2018, 20:26
View user's profile Send private message Reply with quote
vanquish



Joined: 25 Feb 2018
Posts: 3
vanquish 26 Feb 2018, 02:20
@redsock. Thank you for the link will check it out.
Post 26 Feb 2018, 02:20
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2025, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.