NASM Forum > Summer of Code Ideas

Nasm is still quite stupid enough

<< < (2/2)

uncle Richard:
Looking at the endless abundance of high-level languages, one might think that assembler has long died. But this is not so. Suppose you write a library and want to collect all the strings in one object file. Even using the lowest level of all the higher levels - C, nothing will work. All C compilers are the same in this respect. I have an answer why this is happening. But for now, I will refrain from discussing.

The simplest program and assembler for it.

#define STR1 ";string_1\r\n"
#define STR2 ";string_2\r\n"
#define STR3 ";string_3\r\n"
#define STR4 ";string_4\r\n"
#define STR5 ";string_5\r\n"
#define STR6 ";string_6\r\n"
extern int printf();
void _start (void){
   printf(STR1);
   printf(STR2);
   printf(STR5);
}

section code
[global   _start]
_start:
; Line 8:   void _start (void){

L_4:
; Line 9:      printf(STR1);

   push   dword L_1
   call   printf
   pop   ecx
; Line 10:      printf(STR2);

   push   dword L_2
   call   printf
   pop   ecx
; Line 11:      printf(STR5);

   push   dword L_3
   call   printf
   pop   ecx
; Line 12:   }

L_5:
   ret
section data
section code
section data
section string
L_3:
   db   ";string_5"
   db   0dh
   db   0ah
   db   00h
L_2:
   db   ";string_2"
   db   0dh
   db   0ah
   db   00h
L_1:
   db   ";string_1"
   db   0dh
   db   0ah
   db   00h
section const

section code
[extern   printf]


We have 6 strings, and only 3 are in the object file. Let's cut the program down to the can’t.

#define STR1 ";string_1\r\n"
#define STR2 ";string_2\r\n"
#define STR3 ";string_3\r\n"
#define STR4 ";string_4\r\n"
#define STR5 ";string_5\r\n"
#define STR6 ";string_6\r\n"

section code
section data
section code
section data
section const

The object file is full zero. And now we take the assembler and by the hands, hands :)

   SECTION .data
SECTION .data

STR1:
   db   ";string_1", 0dh, 0ah, 00h
STR2:
   db   ";string_2", 0dh, 0ah, 00h
STR3:
   db   ";string_3", 0dh, 0ah, 00h
STR4:
   db   ";string_4", 0dh, 0ah, 00h
STR5:
   db   ";string_5", 0dh, 0ah, 00h
STR6:
   db   ";string_6", 0dh, 0ah, 00h
;.....

uncle Richard:
Life goes on. And it is no secret that Nasm is still quite stupid enough. Now no dll from Nasm/Yasm elf output. Example.

//num_count_1.c
#define EOF (-1)
extern printf();
extern getchar();

_export void bb(void){
   int nc;
   for(nc=0;getchar()!=EOF;++nc);
   printf("%d\n", nc);   }
_export int __dllstart(){bb();
            return 0;}
++++++++++++++++++++++++++++++++++++++

nasm -f elf num_count_1.asm

   SECTION .text
   SECTION .data
   SECTION .bss
SECTION .text
[BITS 32]
[GLOBAL   bb]
bb:
;
; Line 7:   _export void bb(void){
;
   PUSH   EBP
   MOV   EBP,ESP
   SUB   ESP,BYTE 08H
L_3:
   MOV   DWORD [EBP-04H],00H
   JMP   SHORT   L_6
L_4:
;
; Line 9:   for(nc=0;getchar()!=EOF;++nc);
;
L_5:
   INC   DWORD [EBP-04H]
L_6:
   CALL   getchar
   CMP   EAX,BYTE 0FFFFFFFFH
   JNE   SHORT   L_4
L_7:
;
; Line 10:   printf("%d\n", nc);   }
;
   PUSH   DWORD [EBP-04H]
   PUSH   DWORD L_1
   CALL   printf
   ADD   ESP,BYTE 08H
L_2:
   MOV   ESP,EBP
   POP   EBP
   RET
[GLOBAL   __dllstart]
__dllstart:
;
; Line 11:   _export int __dllstart(){bb();
;
   PUSH   EBP
   MOV   EBP,ESP
L_9:
   CALL   bb
;
; Line 12:   return 0;}
;
   MOV   EAX,00H
L_8:
   POP   EBP
   RET
SECTION .data
L_1:
   DB   025H,064H,0AH,00H
SECTION .text
[BITS 32]

SECTION .text
[BITS 32]
   export __dllstart ; parser error
   export bb ; parser error
[EXTERN   getchar]
[EXTERN   printf]

Got parser error & parser error. Maybe elf format have no exports in Windows? Have! Tcc prove it well. Let me guess. Frank Kotler again will say - I am terribly sorry you had trouble with Nasm. O, thank you Frank.:) Longing is green. Black dog. Dumps.

uncle Richard:
Day two. The weather was bad. The princess was wet. I love wet princesses very much. And you? :) Here is a small dump from the previous listing. newdll.o

00000000  7F 45 4C 46 01 01 01 00-00 00 00 00 00 00 00 00  .ELF............
00000010  01 00 03 00 01 00 00 00-00 00 00 00 00 00 00 00  ................
00000020  40 00 00 00 00 00 00 00-34 00 00 00 00 00 28 00  @.......4.....(.
00000030  08 00 04 00 00 00 00 00-00 00 00 00 00 00 00 00  ................
00000040  00 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00  ................
00000050  00 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00  ................
00000060  00 00 00 00 00 00 00 00-01 00 00 00 01 00 00 00  ................
00000070  06 00 00 00 00 00 00 00-80 01 00 00 1C 00 00 00  ................
00000080  00 00 00 00 00 00 00 00-10 00 00 00 00 00 00 00  ................
00000090  07 00 00 00 01 00 00 00-03 00 00 00 00 00 00 00  ................
000000A0  A0 01 00 00 09 00 00 00-00 00 00 00 00 00 00 00  ................
000000B0  04 00 00 00 00 00 00 00-0D 00 00 00 08 00 00 00  ................
000000C0  03 00 00 00 00 00 00 00-B0 01 00 00 04 00 00 00  ................
000000D0  00 00 00 00 00 00 00 00-04 00 00 00 00 00 00 00  ................
000000E0  12 00 00 00 03 00 00 00-00 00 00 00 00 00 00 00  ................
000000F0  B0 01 00 00 36 00 00 00-00 00 00 00 00 00 00 00  ....6...........
00000100  01 00 00 00 00 00 00 00-1C 00 00 00 02 00 00 00  ................
00000110  00 00 00 00 00 00 00 00-F0 01 00 00 B0 00 00 00  ................
00000120  06 00 00 00 08 00 00 00-04 00 00 00 10 00 00 00  ................
00000130  24 00 00 00 03 00 00 00-00 00 00 00 00 00 00 00  $...............
00000140  A0 02 00 00 51 00 00 00-00 00 00 00 00 00 00 00  ....Q...........
00000150  01 00 00 00 00 00 00 00-2C 00 00 00 09 00 00 00  ........,.......
00000160  00 00 00 00 00 00 00 00-00 03 00 00 18 00 00 00  ................
00000170  05 00 00 00 01 00 00 00-04 00 00 00 08 00 00 00  ................
00000180  55 89 E5 6A 40 68 00 00-00 00 FF 35 00 00 00 00  U..j@h.....5....
00000190  6A 00 E8 FC FF FF FF 83-C4 10 5D C3 00 00 00 00  j.........].....
000001A0  46 72 6F 6D 20 44 4C 4C-00 00 00 00 00 00 00 00  From DLL........
000001B0  00 2E 74 65 78 74 00 2E-64 61 74 61 00 2E 62 73  ..text..data..bs
000001C0  73 00 2E 73 68 73 74 72-74 61 62 00 2E 73 79 6D  s..shstrtab..sym
000001D0  74 61 62 00 2E 73 74 72-74 61 62 00 2E 72 65 6C  tab..strtab..rel
000001E0  2E 74 65 78 74 00 00 00-00 00 00 00 00 00 00 00  .text...........
000001F0  00 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00  ................
00000200  01 00 00 00 00 00 00 00-00 00 00 00 04 00 F1 FF  ................
00000210  00 00 00 00 00 00 00 00-00 00 00 00 03 00 01 00  ................
00000220  00 00 00 00 00 00 00 00-00 00 00 00 03 00 02 00  ................
00000230  00 00 00 00 00 00 00 00-00 00 00 00 03 00 03 00  ................
00000240  2E 00 00 00 03 00 00 00-00 00 00 00 00 00 01 00  ................
00000250  32 00 00 00 1A 00 00 00-00 00 00 00 00 00 01 00  2...............
00000260  36 00 00 00 00 00 00 00-00 00 00 00 00 00 02 00  6...............
00000270  23 00 00 00 00 00 00 00-00 00 00 00 10 00 01 00  #...............
00000280  3A 00 00 00 00 00 00 00-00 00 00 00 10 00 03 00  :...............
00000290  45 00 00 00 00 00 00 00-00 00 00 00 10 00 00 00  E...............
000002A0  00 45 3A 5C 00 00 00 00-00 00 00 00 00 00 00 00  .E:\............
000002B0  00 00 00 00 00 00 00 5C-6E 65 77 64 6C 6C 2E 41  .......\newdll.A
000002C0  53 4D 00 68 65 6C 6C 6F-5F 66 75 6E 63 00 4C 5F  SM.hello_func.L_
000002D0  33 00 4C 5F 32 00 4C 5F-31 00 68 65 6C 6C 6F 5F  3.L_2.L_1.hello_
000002E0  64 61 74 61 00 4D 65 73-73 61 67 65 42 6F 78 41  data.MessageBoxA
000002F0  00 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00  ................
00000300  06 00 00 00 01 03 00 00-0C 00 00 00 01 04 00 00  ................
00000310  13 00 00 00 02 0A 00 00-00 00 00 00 00 00 00 00  ................

I made it by hand.:) Therefore, there may be small errors. But I tried and was extremely attentive.:) It is enough to change some bytes and Tcc will accept this file for execution to DLL. If you managed correctly, a def file should appear. newdll.def - LIBRARY newdll.dll EXPORTS hello_data hello_func  Successes.:) I am terribly sorry you had trouble with me, Frank.:)

Navigation

[0] Message Index

[*] Previous page

Go to full version