It is currently Wed Jan 20, 2021 4:29 am

All times are UTC + 1 hour




Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: How to call PureBasic procedures using assembly
PostPosted: Fri Jan 11, 2013 10:48 am 
Offline
User
User

Joined: Fri Jul 27, 2012 8:12 pm
Posts: 12
Location: Italy
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:
extrn _PB_OpenConsole@0

...

; OpenConsole()
  CALL  _PB_OpenConsole@0



Any clues?

Thanks


Top
 Profile  
Reply with quote  
 Post subject: Re: How to call PureBasic procedures using assembly
PostPosted: Fri Jan 11, 2013 9:27 pm 
Offline
Enthusiast
Enthusiast

Joined: Thu May 06, 2010 11:36 pm
Posts: 107
I created an example for Linux 64 Bit and used the following assembler code:
Code:
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:
nasm -o test.o -f elf64 test.asm

and this PB code to call the function and to provide the libraries:
Code:
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

_________________
My blog: http://fds-team.de/cms/


Top
 Profile  
Reply with quote  
 Post subject: Re: How to call PureBasic procedures using assembly
PostPosted: Sun Jan 13, 2013 11:02 am 
Offline
User
User

Joined: Fri Jul 27, 2012 8:12 pm
Posts: 12
Location: Italy
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 :)


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 4 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye