How to resolve the algorithm Call a foreign-language function step by step in the PureBasic programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Call a foreign-language function step by step in the PureBasic programming language

Table of Contents

Problem Statement

Show how a foreign language function can be called from the language.

As an example, consider calling functions defined in the C language. Create a string containing "Hello World!" of the string type typical to the language. Pass the string content to C's strdup. The content can be copied if necessary. Get the result from strdup and print it using language means. Do not forget to free the result of strdup (allocated in the heap).

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Call a foreign-language function step by step in the PureBasic programming language

Source code in the purebasic programming language

; Call_a_foreign_language_function.fasm -> Call_a_foreign_language_function.obj
; the assembler code...

; format COFF or 
; format COFF64 classic (DJGPP) variants of COFF file

; format MS COFF or 
; format MS COFF64 Microsoft's variants of COFF file

format  MS COFF 

include "Win32A.Inc"

section ".text" executable readable code

proc	strucase stdcall str:dword
xor	eax,eax
mov	ebx,[str]
strucase_loop:
mov	al,byte[ebx]
cmp	al,0
jz	strucase_is_null_byte
cmp	al,'a'
jb	strucase_skip
cmp	al,'z'
ja	strucase_skip
and	al,11011111b
strucase_skip:
; mov	byte[ebx],al
xchg	al,byte[ebx]
inc	ebx
jmp	strucase_loop
strucase_is_null_byte:
xor	eax,eax
mov	eax,[str]
ret
endp

public strucase as "_strucase@4"

; the PureBasic code...

Import "Call_a_foreign_language_function.obj"
strucase(t.s) As "_strucase@4"
EndImport

t.s="hElLo WoRld!!"
*r=StrUcase(t.s) ; PureBasic is case-insensitive
; cw(peeks(*r))
Debug peeks(*r)

  

You may also check:How to resolve the algorithm Fibonacci sequence step by step in the C programming language
You may also check:How to resolve the algorithm Hello world/Web server step by step in the Standard ML programming language
You may also check:How to resolve the algorithm 100 prisoners step by step in the CLU programming language
You may also check:How to resolve the algorithm 100 prisoners step by step in the Scala programming language
You may also check:How to resolve the algorithm Sequence: smallest number with exactly n divisors step by step in the F# programming language