Hi,

i just wanna ask about compiling asm code with gcc.
Is it any difference syntax between compiling asm code using gcc and common asm compiler.
for example, this common code below:
---------------------------------------------
.model small
.stack
.data
message  db "Hello world, I'm learning Assembly !!!", "$"

.code

main  proc
  mov  ax,seg message
  mov  ds,ax

  mov  ah,09
  lea  dx,message
  int  21h

  mov  ax,4c00h
  int  21h
main  endp
end main
----------------------------------

i try to compile it with gcc -c -o aa aa.S
and i found this errors:

aa.S: Assembler messages:
aa.S:1: Error: unknown pseudo-op: `.model'
aa.S:2: Error: unknown pseudo-op: `.stack'
aa.S:4: Error: no such instruction: `message db "Hello world, I'm learning Assembly !!!","$"'
aa.S:6: Error: unknown pseudo-op: `.code'
aa.S:8: Error: no such instruction: `main proc'
aa.S:9: Error: too many memory references for `mov'
aa.S:10: Error: too many memory references for `mov'
aa.S:12: Error: too many memory references for `mov'
aa.S:13: Error: too many memory references for `lea'
aa.S:14: Error: junk `h' after expression
aa.S:14: Error: suffix or operands invalid for `int'
aa.S:16: Error: too many memory references for `mov'
aa.S:17: Error: junk `h' after expression
aa.S:17: Error: suffix or operands invalid for `int'
aa.S:18: Error: no such instruction: `main endp'
aa.S:19: Error: no such instruction: `end main'

is there any one knows the answer ? Thx.
Posted on 2009-07-07 01:05:20 by morningboy
You have to specifically instruct GCC/GAS to use intel-style syntax instead of AT&T. However, even the particular intel-style syntax that GCC/GAS accepts is rather different from your code.

A more favorable option would be to assemble with a MASM/TASM compatible assembler, and then use a 16-bit linker.
Posted on 2009-07-07 01:45:26 by SpooK
An example of GNU assembler with Intel syntax.

FILE : Agner.s
Assemble like that :
as agner.s



The file :
	.file	"agner.cpp"
.intel_syntax noprefix
.section .rdata,"dr"
LC0:
.ascii "Combien de memoire?\0"
LC1:
.ascii "%d\0"
LC2:
.ascii "Il veut %d en memoire\0"
LC3:
.ascii "\12%d\0"
LC4:
.ascii "String a copier:\0"
LC5:
.ascii "%s\0"
.text
.p2align 4,,15
.globl _appstart
.def _appstart; .scl 2; .type 32; .endef
_appstart:
LFB82:
push ebp
LCFI0:
mov ebp, esp
LCFI1:
push ebx
LCFI2:
sub esp, 36
LCFI3:
mov DWORD PTR , OFFSET FLAT:LC0
call _printf
lea eax,
mov DWORD PTR , eax
mov DWORD PTR , OFFSET FLAT:LC1
call _scanf
mov eax, DWORD PTR
mov DWORD PTR , eax
call _malloc
mov DWORD PTR , 15
mov ebx, eax
mov eax, DWORD PTR
mov DWORD PTR , ebx
mov DWORD PTR , eax
call _memset
mov eax, DWORD PTR
mov DWORD PTR , OFFSET FLAT:LC2
mov DWORD PTR , ebx
mov DWORD PTR , eax
call _sprintf
mov DWORD PTR , ebx
mov DWORD PTR , OFFSET FLAT:__ZZ8appstartE6buffer
call _strcpy
mov DWORD PTR , ebx
call _printf
mov eax, DWORD PTR __imp____mb_cur_max
cmp DWORD PTR , 1
je L6
mov DWORD PTR , 128
mov DWORD PTR , 70
call __isctype
L3:
mov DWORD PTR , eax
mov DWORD PTR , OFFSET FLAT:LC3
call _printf
mov DWORD PTR , OFFSET FLAT:LC4
call _printf
mov DWORD PTR , OFFSET FLAT:__ZZ8appstartE7buffer2
mov DWORD PTR , OFFSET FLAT:LC5
call _scanf
mov DWORD PTR , OFFSET FLAT:__ZZ8appstartE7buffer2
mov DWORD PTR , OFFSET FLAT:__ZZ8appstartE7buffer3
call _strcpy
mov DWORD PTR , OFFSET FLAT:__ZZ8appstartE7buffer3
call _printf
mov DWORD PTR , 0
call _ExitProcess@4
xor eax, eax
mov ebx, DWORD PTR
sub esp, 4
leave
ret
.p2align 4,,7
L6:
mov eax, DWORD PTR __imp___pctype
mov eax, DWORD PTR
movzx eax, WORD PTR
and eax, 128
jmp L3
LFE82:
.lcomm __ZZ8appstartE7buffer3,1008
.lcomm __ZZ8appstartE7buffer2,512
.lcomm __ZZ8appstartE6buffer,256
.section .eh_frame,"dr"
Lframe1:
.long LECIE1-LSCIE1
LSCIE1:
.long 0x0
.byte 0x1
.ascii "zP\0"
.uleb128 0x1
.sleb128 -4
.byte 0x8
.uleb128 0x5
.byte 0x0
.byte 0xc
.uleb128 0x4
.uleb128 0x4
.byte 0x88
.uleb128 0x1
.align 4
LECIE1:
LSFDE1:
.long LEFDE1-LASFDE1
LASFDE1:
.long LASFDE1-Lframe1
.long LFB82
.long LFE82-LFB82
.uleb128 0x0
.byte 0x4
.long LCFI0-LFB82
.byte 0xe
.uleb128 0x8
.byte 0x85
.uleb128 0x2
.byte 0x4
.long LCFI1-LCFI0
.byte 0xd
.uleb128 0x5
.byte 0x4
.long LCFI3-LCFI1
.byte 0x83
.uleb128 0x3
.align 4
LEFDE1:
.def __isctype; .scl 2; .type 32; .endef
.def _malloc; .scl 2; .type 32; .endef
.def _memset; .scl 2; .type 32; .endef
.def _sprintf; .scl 2; .type 32; .endef
.def _scanf; .scl 2; .type 32; .endef
.def _strcpy; .scl 2; .type 32; .endef
.def _printf; .scl 2; .type 32; .endef
.def _ExitProcess@4; .scl 2; .type 32; .endef


You can then link this file using GNU linker LD or link.exe or polink.exe.

And Spook is right, his advice to you is good.
Posted on 2009-07-07 07:33:59 by ChaperonNoir
ugly code!
Posted on 2009-07-07 08:52:19 by roticv
Yea, it's very ugly. The intel syntax is a recent blessing because it didn't exist before.
I still like visual C++ and Intel C++ assembly output of C programs better.
Posted on 2009-07-07 13:59:04 by ChaperonNoir