Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-devel / 2007-04

GNU assembler programming

4 messages · 1 person ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Felipe Monteiro de Carvalho

hello,

Working on the symbian os port, one of the greatest difficulties I am facing is to work with the Gnu assembler. I am trying to generate a trivial win32 assembler app, but it´s much more difficult then it google, google, google, but I just can´t find a
simple example of a win32 app made for gnu assembler.

Basically I want to write assembler apps for the emulator and then compare my apps to the ones generated by fpc to find what is wrong.

I also couldn´t find anywhere to ask question about gnu assembler programming, so if anyone knows such a place, please point me to it. In the mean time I would be very thankful if anyone here that knows how to use gas could help me =)

At the moment I am trying to call a winapi function from gas. I know how to declare external functions on pascal, but not on assembler.

Here is my test app:

// Hello world to create a symbian os executable on assembler
	.file   "hello.as"

.section .data
.balign 16
mymessage:
.ascii "My Message"

.text
/* Entry point for Symbian OS executables */
.globl _E32EXE
_E32EXE:
/* Entry point for MS Windows */
.globl main
main:
MOV $0, %EAX
PUSH %EAX
MOV mymessage, %EAX
PUSH %EAX
MOV $0, %EAX
PUSH %EAX
PUSH %EAX
CALL MessageBox
LEAVE
RET
NOP
NOP

And how I build it:

C:\Programas\lazarus20\fpc\2.0.4\bin\i386-win32\as hello.as -o hello.o C:\Programas\lazarus20\fpc\2.0.4\bin\i386-win32\ld.exe --entry=_E32EXE -o hello.exe hello.o

The error is that MessageBox isn´t declared.

thanks,

Felipe Monteiro de Carvalho

Reply by email

Felipe Monteiro de Carvalho
to Felipe Monteiro de Carvalho

Hello,

I taked to Florian and he explained everything, so I can answer my own question for others that search this later =)

Basically fpc writes the idata sections manually, and that is the import table on pe executables. Here is a example app that links to ExitProcess from kernel32 dll (I know there are some things wrong with the app, but it does work).

/* Hello world to create a symbian os executable on assembler */

.file "hello.s"
.text
/* Variables */
mymessage:
.ascii "My Message"

/* Entry point for Symbian OS executables */
.globl _E32EXE
_E32EXE:
/* Entry point for MS Windows */
.globl main
main:
/* Entry point for GCC */
.globl

MOV $0, %EAX
PUSH %EAX
MOV $mymessage, %EAX
PUSH %EAX
MOV $0, %EAX
PUSH %EAX
PUSH %EAX
CALL MessageBox
MOV $0, %EAX
LEAVE
RET
NOP
NOP

.section .text
.balign 4

.section .idata$2
.rva .L16
.long 0
.long 0
.rva .L15
.rva .L17

.section .idata$4
.L16:
.rva .L18
.long 0

.section .idata$5
.L17:

.section .text
.globl MessageBox
MessageBox:
jmp *.L19
.balign 4,144

.section .idata$5
.L19:
.rva .L18
.long 0

.section .idata$6
.L18:
.short 0
.ascii "ExitProcess\000"
.balign 2,0

.section .idata$7
.L15:
.ascii "kernel32.dll\000"

Reply by email

Felipe Monteiro de Carvalho
to Felipe Monteiro de Carvalho

Ok, maybe I´m too happy for making a asm app and writing my own idata section, but here is another example =)

It shows a message box and then quit.

/* Hello world to create a symbian os executable on assembler */

.file "hello.s"
.text
/* Variables */
mymessage:
.ascii "My Message\000"

/* Entry point for Symbian OS executables */
.globl _E32EXE
_E32EXE:
/* Entry point for MS Windows */
.globl main
main:
/* Entry point for GCC */
.globl

MOV $0, %EAX
PUSH %EAX
MOV $mymessage, %EAX
PUSH %EAX
MOV $0, %EAX
PUSH %EAX
PUSH %EAX
CALL MessageBoxA
MOV $0, %EAX
PUSH %EAX
CALL ExitProcess
LEAVE
RET
NOP
NOP

.section .text
.balign 4

.section .idata$2
.rva .L18
.long 0
.long 0
.rva .L17
.rva .L19

.section .idata$4
.L18:
.rva .L20
.long 0

.section .idata$5
.L19:

.section .text
.globl ExitProcess
ExitProcess:
jmp *.L21
.balign 4,144

.section .idata$5
.L21:
.rva .L20
.long 0

.section .idata$6
.L20:
.short 0
.ascii "ExitProcess\000"
.balign 2,0

.section .idata$7
.L17:
.ascii "kernel32.dll\000"

.section .text
.balign 4

.section .idata$2
.rva .L23
.long 0
.long 0
.rva .L22
.rva .L24

.section .idata$4
.L23:
.rva .L25
.long 0

.section .idata$5
.L24:

.section .text
.globl MessageBoxA
MessageBoxA:
jmp *.L26
.balign 4,144

.section .idata$5
.L26:
.rva .L25
.long 0

.section .idata$6
.L25:
.short 0
.ascii "MessageBoxA\000"
.balign 2,0

.section .idata$7
.L22:
.ascii "user32.dll\000"

Reply by email

Felipe Monteiro de Carvalho
to Felipe Monteiro de Carvalho

Hi,

Today is a great day of the symbian port =) I also solved my other problem. I learned how to use the .a files. They should be added on the ld command line together with the other .o files, and not as lib files.

So, here is the same program using the .a archives from DevC++:

Compile script:

C:\Programas\lazarus20\fpc\2.0.4\bin\i386-win32\as hello.s -o hello.o C:\Programas\lazarus20\fpc\2.0.4\bin\i386-win32\ld.exe --entry=_E32EXE -o hello.exe hello.o C:\Programas\Dev-Cpp\lib\libkernel32.a C:\Programas\Dev-Cpp\lib\libuser32.a

Code:

/* Hello world to create a symbian os executable on assembler */

.file "hello.s"
.text
/* Variables */
mymessage:
.ascii "My Message\000"

/* Entry point for Symbian OS executables */
.globl _E32EXE
_E32EXE:
/* Entry point for MS Windows */
.globl main
main:
/* Entry point for GCC */
.globl

MOV $0, %EAX
PUSH %EAX
MOV $mymessage, %EAX
PUSH %EAX
MOV $0, %EAX
PUSH %EAX
PUSH %EAX
CALL
MOV $0, %EAX
PUSH %EAX
CALL
LEAVE
RET
NOP
NOP

Reply by email