flat assembler
Message board for the users of flat assembler.
Index
> DOS > Static Library file for C in FASM |
Author |
|
Kitsune 16 Jun 2024, 23:50
Ok I created a simple Hello World message with Fasm, C and DJGPP on DOS
I'm so happy I will put the code here! 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
_________________ Kitsune |
||||||||||
16 Jun 2024, 23:50 |
|
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)
_________________ Kitsune Last edited by Kitsune on 17 Jun 2024, 19:06; edited 1 time in total |
||||||||||
17 Jun 2024, 16:31 |
|
SeproMan 17 Jun 2024, 18:34
In SUM.ASM there's one instruction that is wrong.
Code: add eax,ebp Code: add eax,[ebp+12] |
|||
17 Jun 2024, 18:34 |
|
Kitsune 17 Jun 2024, 19:08
Correct. It's a mistake I have done by copied this code here. Thanks
|
|||
17 Jun 2024, 19:08 |
|
MatQuasar2 18 Jun 2024, 05:37
Kitsune wrote:
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,"$" |
|||
18 Jun 2024, 05:37 |
|
Kitsune 18 Jun 2024, 14:32
Thanks I tested this same code in my function but I have SIgsev error appeared.
_________________ Kitsune |
|||||||||||||||||||
18 Jun 2024, 14:32 |
|
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.
|
|||
18 Jun 2024, 14:48 |
|
MatQuasar2 18 Jun 2024, 15:09
And I think don't need these two lines:
Quote: mov ax, 4c00h It is used to terminate the program. |
|||
18 Jun 2024, 15:09 |
|
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 |
|||
18 Jun 2024, 15:34 |
|
revolution 18 Jun 2024, 15:49
Try:
Code: lea dx,[hello] |
|||
18 Jun 2024, 15:49 |
|
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.
_________________ Kitsune |
||||||||||||||||||||||||||||
18 Jun 2024, 16:10 |
|
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. |
|||
18 Jun 2024, 16:36 |
|
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,"$" |
|||
18 Jun 2024, 17:19 |
|
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 |
|||
18 Jun 2024, 17:30 |
|
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 |
|||
18 Jun 2024, 17:41 |
|
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). |
|||
18 Jun 2024, 18:03 |
|
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.
|
|||
18 Jun 2024, 18:07 |
|
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
_________________ Kitsune |
|||||||||||||||||||
18 Jun 2024, 18:30 |
|
< Last Thread | Next Thread > |
Forum Rules:
|
Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.
Website powered by rwasa.