flat assembler
Message board for the users of flat assembler.

Index > DOS > Static Library file for C in FASM

Author
Thread Post new topic Reply to topic
Kitsune



Joined: 17 Apr 2023
Posts: 18
Kitsune 16 Jun 2024, 19:42
Hi,

I don't find any tutorial about creating a Static library for DOS environment about mixed C with FASM.

Can you make a simple Hello Word function in a library for C with FASM?
Can you make a 2 int sum in function in a library for C FASM?

I use DJGPP for compiling C project on MS-DOS.

Thanks.

_________________
Kitsune
Post 16 Jun 2024, 19:42
View user's profile Send private message Reply with quote
Kitsune



Joined: 17 Apr 2023
Posts: 18
Kitsune 16 Jun 2024, 23:50
Ok I created a simple Hello World message with Fasm, C and DJGPP on DOS Very Happy
I'm so happy I will put the code here! Razz

COFF.ASM
Code:
format MS COFF

public hello as '_hello'

;data section
section '.data' data readable
    hello db "Hello World!",0
    


MIXED.C
Code:
#include <stdio.h>

extern char hello[];
int main()
{
    puts(hello);
    return 0;
}
    


fasm coff.asm coff.obj
gcc coff.obj mixed.c -o mixed.exe

Proove of concept on the printscreen Smile


Description:
Filesize: 24.12 KB
Viewed: 1984 Time(s)

Capture d’écran du 2024-06-17 01-49-27.png



_________________
Kitsune
Post 16 Jun 2024, 23:50
View user's profile Send private message Reply with quote
Kitsune



Joined: 17 Apr 2023
Posts: 18
Kitsune 17 Jun 2024, 16:31
Here another example I find on the web. I converting from NASM to FASM how I can.

It's a function in assembly that make a sum of 2 integers and access it by C programming language.

SUM.ASM
Code:
format COFF
use32

public sum as '_sum'
section '.text'

sum:
    push ebp
    mov ebp, esp
    mov eax, [ebp+8]
    add eax, [ebp+12]
    pop ebp

    ret
    


SUM.C
Code:
#include <stdio.h>
extrn int sum(int,int)

int main(void){
    int a,b,answer;
    printf("Enter two integers separated by a space");
    scanf("%d %d",&a,&b);

    answer=sum(a,b);
    printf'("%d\n",answer);
    return 0;
}
    


But I don't have find a way to implement a function that can diplay a string directly in assembly by a COFF file for now. If someone have an idea about it?
(I don't want to call a C function for printing on screen a string)


Description:
Filesize: 38.6 KB
Viewed: 1943 Time(s)

Capture d’écran du 2024-06-17 18-29-50.png



_________________
Kitsune


Last edited by Kitsune on 17 Jun 2024, 19:06; edited 1 time in total
Post 17 Jun 2024, 16:31
View user's profile Send private message Reply with quote
SeproMan



Joined: 11 Oct 2009
Posts: 70
Location: Belgium
SeproMan 17 Jun 2024, 18:34
In SUM.ASM there's one instruction that is wrong.
Code:
add eax,ebp    
should be
Code:
add eax,[ebp+12]    
.
Post 17 Jun 2024, 18:34
View user's profile Send private message Reply with quote
Kitsune



Joined: 17 Apr 2023
Posts: 18
Kitsune 17 Jun 2024, 19:08
Correct. It's a mistake I have done by copied this code here. Thanks
Post 17 Jun 2024, 19:08
View user's profile Send private message Reply with quote
MatQuasar2



Joined: 10 Jun 2024
Posts: 26
MatQuasar2 18 Jun 2024, 05:37
Kitsune wrote:

But I don't have find a way to implement a function that can diplay a string directly in assembly by a COFF file for now. If someone have an idea about it?
(I don't want to call a C function for printing on screen a string)


Hi nice two examples by you.

To print string in DOS, one of the function you can use is AH=9 Int 21h.

Below is the example "Hello.com" but I am not sure how to do it for COFF:
Code:
org 100h

mov     dx, hello
mov     ah, 9
int     21h

mov     ax, 4c00h
int     21h

hello   db "Hello World",13,10,"$"          
Post 18 Jun 2024, 05:37
View user's profile Send private message Reply with quote
Kitsune



Joined: 17 Apr 2023
Posts: 18
Kitsune 18 Jun 2024, 14:32
Thanks I tested this same code in my function but I have SIgsev error appeared.


Description:
Filesize: 22.62 KB
Viewed: 1859 Time(s)

Capture d’écran du 2024-06-18 16-32-23.png


Description:
Filesize: 30.64 KB
Viewed: 1859 Time(s)

Capture d’écran du 2024-06-18 16-30-11.png



_________________
Kitsune
Post 18 Jun 2024, 14:32
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20299
Location: In your JS exploiting you and your system
revolution 18 Jun 2024, 14:48
You need to delete the "org 100h" line. The linker will set the addresses, no need to define them in the COFF file.
Post 18 Jun 2024, 14:48
View user's profile Send private message Visit poster's website Reply with quote
MatQuasar2



Joined: 10 Jun 2024
Posts: 26
MatQuasar2 18 Jun 2024, 15:09
And I think don't need these two lines:

Quote:
mov ax, 4c00h
int 21h


It is used to terminate the program.
Post 18 Jun 2024, 15:09
View user's profile Send private message Reply with quote
Kitsune



Joined: 17 Apr 2023
Posts: 18
Kitsune 18 Jun 2024, 15:34
I agree with it about erasing the theses 2 lines about Quitting the program cause it's inside a function.

revolution wrote:
You need to delete the "org 100h" line. The linker will set the addresses, no need to define them in the COFF file.


But if I erase org 100h that's stop compiling the program at line

Code:
mov dx, hello    

with error:
Quote:
invalid use of symbol


if I put this instead of previous code it compile
Code:
mov edx, hello    


But I have the same kind of SIGSEV error with it.
So Is there really a way to print a string without calling a C function in a OBJ file from DOS environment?

_________________
Kitsune
Post 18 Jun 2024, 15:34
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20299
Location: In your JS exploiting you and your system
revolution 18 Jun 2024, 15:49
Try:
Code:
lea dx,[hello]    
Post 18 Jun 2024, 15:49
View user's profile Send private message Visit poster's website Reply with quote
Kitsune



Joined: 17 Apr 2023
Posts: 18
Kitsune 18 Jun 2024, 16:10
Ok I have revisited my own code...SIGSEV error has been corriged I will put a screen of the code with lea for sure.

Same trick with Lea...the output make randomly caracters now but the app no crash.


Description: Lea
Filesize: 19.73 KB
Viewed: 1817 Time(s)

Capture d’écran du 2024-06-18 18-16-52.png


Description:
Filesize: 25 KB
Viewed: 1818 Time(s)

Capture d’écran du 2024-06-18 18-15-55.png


Description:
Filesize: 19.74 KB
Viewed: 1820 Time(s)

Capture d’écran du 2024-06-18 18-09-21.png



_________________
Kitsune
Post 18 Jun 2024, 16:10
View user's profile Send private message Reply with quote
MatQuasar2



Joined: 10 Jun 2024
Posts: 26
MatQuasar2 18 Jun 2024, 16:36
I don't know how to do it COFF (with different section), but in DOS EXE with different segment, this is how I do it:

Code:
format MZ

segment A

mov  ax, B
mov  ds, ax
lea  dx, [hello]
mov  ah, 9              ;print string
int  21h

mov  ax, 4c00h          ;exit
int  21h

segment B

hello db "Hello World",13,10,"$"
                                           


...by pointing the DS (data segment) to "B" where "hello" string is resided.
Post 18 Jun 2024, 16:36
View user's profile Send private message Reply with quote
MatQuasar2



Joined: 10 Jun 2024
Posts: 26
MatQuasar2 18 Jun 2024, 17:19
Or maybe you can try moving your "hello" string to the same section as code and then call these two lines in the beginning:

Code:
push cs
pop ds
    


The complete EXE with single segment is as follows: (again, I am not familiar with COFF)
Code:
format MZ

segment A

push cs
pop  ds
lea  dx, [hello]   
mov  ah, 9              ;print string
int  21h

mov  ax, 4c00h          ;exit
int  21h

hello db "Hello World",13,10,"$"         
Post 18 Jun 2024, 17:19
View user's profile Send private message Reply with quote
bitRAKE



Joined: 21 Jul 2003
Posts: 4020
Location: vpcmpistri
bitRAKE 18 Jun 2024, 17:30
"use32" will change how instructions are encoded - unless you're running in a 32-bit mode the programs will not operate as expected.

_________________
¯\(°_o)/¯ “languages are not safe - uses can be” Bjarne Stroustrup
Post 18 Jun 2024, 17:30
View user's profile Send private message Visit poster's website Reply with quote
Kitsune



Joined: 17 Apr 2023
Posts: 18
Kitsune 18 Jun 2024, 17:41
I attempt some tricks with the information you gave to me but I have some SIGSEV error about PUSH and POP..I must put use16 in the start of the code for compile it also.

But thanks.

Can it due to gcc compilator? Cause DJGPP make 32 bits executable so can it work in 32bits with COFF file in 16bits?

_________________
Kitsune
Post 18 Jun 2024, 17:41
View user's profile Send private message Reply with quote
macomics



Joined: 26 Jan 2021
Posts: 926
Location: Russia
macomics 18 Jun 2024, 18:03
To begin with, since you are clearly swimming with processor operating modes and, accordingly, using the use16/32/64 directives by the poke method, I would advise switching from using C + fasm to pure fasm. Create MZ programs on fasm at once and figure out the operating modes of the processor.

The use directives themselves do not switch the processor's operating modes. They switch the command generation mode of the fasm (what will be written to the file as instructions). When using the use16 directive, each command using 32-bit operands will be prefixed. Exactly the same is true and vice versa (for the use32 directive and 16-bit operands).
Post 18 Jun 2024, 18:03
View user's profile Send private message Reply with quote
bitRAKE



Joined: 21 Jul 2003
Posts: 4020
Location: vpcmpistri
bitRAKE 18 Jun 2024, 18:07
It's probably been 20+ years since I've used DJGPP, but mixing of 16-bit and 32-bit code is probably the source of the error. You can use 32-bit registers in 16-bit mode (DOS default) - they are just encoded different. DJGPP does use a DOS extender for 32-bit code, but then you'd want to use 32-bit PUSH/POP.
Post 18 Jun 2024, 18:07
View user's profile Send private message Visit poster's website Reply with quote
Kitsune



Joined: 17 Apr 2023
Posts: 18
Kitsune 18 Jun 2024, 18:30
Macomics you are right, I don't don't have enough knowledge of assembly language and I try to make this trick work by curiosity but it seem get over me.

I'am on a way after that:

This is not in FASM assembly language but I find a way to print a character in C and Assembly using VGA mode 13h:

Code:
void set_vga_mode(unsigned char mode) {
    asm(
        "sti;\
        mov $0x00, %%ah;\
        int $0x10;\
        cli;"
        :
        : "al" (mode)
    );
}
void getch(void){
        asm(
             "xor %ah,%ah;\
              int $0x16;"
           );
}
void printChar(unsigned char charact){
            asm(
               "mov $0x0E,%%ah;\
                int $0x10;"
                :
                : "al" (charact)
              );
}
int main()
{
        char quote[20]="Hello World!\0";
        int cpt=0;
        set_vga_mode(0x13);
        while(quote[cpt]!='\0'){
                        printChar(quote[cpt]);
                        cpt++;
                };
        getch();
        set_vga_mode(0x03);
    return 0; 
}    


(Compiled with GCC from DJGPP)

One hour later --> Translated to FASM ASM COFF file and GCC from DJGPP C file that's done!
FASMVGA.ASM
Code:
format coff


public vgaMode as '_vgaMode'
public getch as '_getch'
public printChar as '_printChar'


section '.text' code

vgaMode:
        push ebp
        mov ebp, esp
        mov al,[ebp+8]
        xor ah,ah
        int 10h
        pop ebp
        ret

getch:
        xor ah,ah
        int 16h
        ret

printChar:
        push ebp
        mov ebp,esp
        mov al,[ebp+8]
        mov ah,0eh
        int 10h
        pop ebp
        ret
    


FASMGCC.C
Code:
extern void vgaMode(unsigned char mode);
extern void getch(void);
extern void printChar(unsigned char charact);

int main()
{
        char quote[20]="Hello World!\0";
        int cpt=0;
        vgaMode(0x13);
        while(quote[cpt]!='\0'){
                        printChar(quote[cpt]);
                        cpt++;
                };
        getch();
        vgaMode(0x03);
    return 0; 
}
    


compile.bat
Code:
cwsdpmi
fasm fasmvga.asm
gcc fasmvga.obj fasmgcc.c -o test.exe
test.exe
    

_______________________________________________________________
Another ONE in VGA: Clear Screen in protected mode with DJGPP and FASM
******************************************************************************************
FASMGCC.C
Code:
#include <stdio.h>
#include <string.h>
#include <sys/nearptr.h>
#define byte unsigned char

byte *VGA=(byte*)0xA0000;

extern void vgaMode(unsigned char mode);
extern void putPixelSlow(void);
extern void putPixel(byte *,int);
extern void getch(void);
extern void printChar(unsigned char charact);

int main()
{
        char quote[50]="Choose a color between 0-255: \0";
        int cpt=0,c=15;

        while(quote[cpt]!='\0'){
                        printChar(quote[cpt]);
                        cpt++;
        };

        scanf("%d",&c);
       
        __djgpp_nearptr_enable();
        VGA+=__djgpp_conventional_base;

        vgaMode(0x13);
        
        putPixel(VGA,c);
       
        cpt=0;
        strncpy(quote,"It's a me\0",50);
        while(quote[cpt]!='\0'){
                      printChar(quote[cpt]);
                        cpt++;
        };
        getch();
        vgaMode(0x03);
        __djgpp_nearptr_disable();
    return 0; 
}    


FASMVGA.ASM

Code:
format coff

public vgaMode as '_vgaMode'
public getch as '_getch'
public printChar as '_printChar'
public putPixel as '_putPixel'

section '.text' code

vgaMode:
        push ebp
        mov ebp, esp
        mov al,[ebp+8]
        xor ah,ah
        int 10h
        pop ebp
        ret
;put Pixel into VRAM
putPixel:
        push ebp
        mov ebp,esp
        mov eax,[ebp+8]
        mov dx,[ebp+12]
        xor ecx,ecx
pixLoop:
        mov [eax+ecx],dx
        inc ecx
        cmp ecx,64000
        jl pixLoop
        pop ebp
        ret
getch:
        xor ah,ah
        int 16h
        ret

printChar:
        push ebp
        mov ebp,esp
        mov al,[ebp+8]
        ;mov bx,0xF
        mov bl,5; Get Foreground color
        mov ah,0eh
        int 10h
        pop ebp
        ret
    


Description: Fasm VGA Clear Screen
Filesize: 9.71 KB
Viewed: 1585 Time(s)

Capture d’écran du 2024-06-20 12-59-50.png


Description:
Filesize: 10.43 KB
Viewed: 1639 Time(s)

Capture d’écran du 2024-06-20 00-19-54.png



_________________
Kitsune
Post 18 Jun 2024, 18:30
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-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.