How to call PureBasic procedures using assembly

Bare metal programming in PureBasic, for experienced users
itto
User
User
Posts: 12
Joined: Fri Jul 27, 2012 8:12 pm
Location: Italy
Contact:

How to call PureBasic procedures using assembly

Post by itto »

Hi everyone, I'm in the process of learning assembly and I'm stuck when trying to call a purebasic procedure using CALL.
I checked the ASM output generated by the compiler and I see all the function used are declared with the extrn keyword and prefixed with _PB_, but when I try to reproduce the same code, I get a polink error of unresolved external symbol (?) for every procedure.
I'm doing this since I want to go low level as much as possible, but at least have to open up a console and print out some strings. So I would like to be able to call the OpenConsole, Print and Input at least...

For example this is the compiler generated asm for the OpenConsole

Code: Select all

extrn _PB_OpenConsole@0

...

; OpenConsole()
  CALL  _PB_OpenConsole@0

Any clues?

Thanks
DarkPlayer
Enthusiast
Enthusiast
Posts: 107
Joined: Thu May 06, 2010 11:36 pm

Re: How to call PureBasic procedures using assembly

Post by DarkPlayer »

I created an example for Linux 64 Bit and used the following assembler code:

Code: Select all

extern PB_OpenConsole
extern PB_Input

global testcode

SECTION .text
testcode:
	call PB_OpenConsole
	cmp rax, 0
	je .end
	call PB_Input
	.end:
	ret
compiled with nasm

Code: Select all

nasm -o test.o -f elf64 test.asm
and this PB code to call the function and to provide the libraries:

Code: Select all

Import "test.o"
	testcode.i()
EndImport

;This procedure is needed to include the needed libraries
Procedure dummyProcedure()
	OpenConsole()
	Input()
EndProcedure

testcode()
The assembler code opens a console and waits for user input. It should also work on Windows with some minor changes.

You need to write all needed PB functions in a dummy procedure or at a different position, which is never executed (this time it is an advantage that the PB compiler does not detect dead code), otherwise PureBasic may not include the needed Libraries while linking the executable. This is not necessary if you compile your code as PureBasic User Library.

I would not recommend you to use this solution with strings, because PureBasic uses it's very own and complicated way to handle strings. PureBasic does not pass strings as simple pointers like the rest of the world, but instead a big global buffer is used. If you pass multiple strings to a function, all strings are copied in this buffer after each other and only the index is passed to the function. There is no real documentation about all this stuff, so it would be much easier to directly use the system API and write some assembler string handling functions on your own.

DarkPlayer
itto
User
User
Posts: 12
Joined: Fri Jul 27, 2012 8:12 pm
Location: Italy
Contact:

Re: How to call PureBasic procedures using assembly

Post by itto »

I see... looks like a bit complicated in the end. Better to stick with PB functions the PB way, and only create new functions in assembly.

Thanks for your reply :)
Post Reply