flat assembler
Message board for the users of flat assembler.
Index
> Unix > How to translate this code to fasmg? |
Author |
|
TmX 21 May 2022, 16:53
I found this code example:
Code: format ELF64 section '.text' executable public main extrn printf extrn exit ;macro for create .size constant automatically struc db [data]{ common . db data .size = $ - . } ;testing using syscall main: mov rax, 0x2000004 ; sys_write mov rdi, 1 ; stdout mov rsi, qword msgHelloWorld ; string mov rdx, msgHelloWorld.size ; length syscall mov rax, 0x2000001 ; sys_exit xor rdi, rdi ; exit code syscall section '.data' writeable msgHelloWorld db 'Hello 64bit World from FASM!',0x0A,0 My first translation attempt: Code: format MachO executable include 'selfhost.inc' section '.text' executable public main extrn printf extrn exit ;macro for create .size constant automatically struc db [data]{ common . db data .size = $ - . } ;testing using syscall main: mov rax, 0x2000004 ; sys_write mov rdi, 1 ; stdout mov rsi, qword msgHelloWorld ; string mov rdx, msgHelloWorld.size ; length syscall mov rax, 0x2000001 ; sys_exit xor rdi, rdi ; exit code syscall section '.data' writeable msgHelloWorld db 'Hello 64bit World from FASM!',0x0A,0 The result is: Quote:
How to fix this? I'm on MacOS Big Sur 11.6.6 |
|||
21 May 2022, 16:53 |
|
Tomasz Grysztar 21 May 2022, 17:29
In order to have that FORMAT option available you need to include format.inc, either one that comes with fasmg core examples, or the one from the fasmg compatibility package.
Set up the INCLUDE variable to point to either of these packages and then include the format/format.inc - you can add the INCLUDE on top of the source file, or you can add it directly from the command line when assembling: Code: fasmg hello64.asm -iInclude\ \'format/format.inc\' And your source requires at least a few tweaks, and conversion of the fasm macro syntax: Code: format MachO64 executable segment '__TEXT' readable executable section '__text' align 16 ;macro for create .size constant automatically struc db data& . db data .size = $ - . end struc ;testing using syscall main: mov rax, 0x2000004 ; sys_write mov rdi, 1 ; stdout mov rsi, qword msgHelloWorld ; string mov rdx, msgHelloWorld.size ; length syscall mov rax, 0x2000001 ; sys_exit xor rdi, rdi ; exit code syscall segment '__DATA' readable writable section '__data' align 4 msgHelloWorld db 'Hello 64bit World from FASM!',0x0A,0 You could also use a different starting point - the Mach-O example published in the official repository. Note that it uses the formatter directly, without the FORMAT directive emulation. But it too requires the compatibility package to be available through the INCLUDE path (you can make a shell wrapper for fasmg that would set up INCLUDE variable locally for the assembly). |
|||
21 May 2022, 17:29 |
|
revolution 22 May 2022, 12:16
I have no experience with Mac but this line worries me:
Code: mov rsi, qword msgHelloWorld ; string Code: lea rsi, [msgHelloWorld] ; string |
|||
22 May 2022, 12:16 |
|
Tomasz Grysztar 22 May 2022, 12:36
TmX wrote: Perhaps I'm missing something? |
|||
22 May 2022, 12:36 |
|
TmX 22 May 2022, 13:35
Like this?
Code: include 'format/format.inc' format MachO64 executable segment '__TEXT' readable executable entry $, rdi: 1, rsi: msgHelloWorld section '__text' align 16 ;macro for create .size constant automatically struc db data& . db data .size = $ - . end struc ;testing using syscall main: mov rax, 0x2000004 ; sys_write mov rdi, 1 ; stdout mov rsi, qword msgHelloWorld ; string mov rdx, msgHelloWorld.size ; length syscall mov rax, 0x2000001 ; sys_exit xor rdi, rdi ; exit code syscall segment '__DATA' readable writable section '__data' align 4 msgHelloWorld db 'Hello 64bit World from FASM!',0x0A,0 BTW, I also tried building the demo_syscall64.asm example as is. Both produced same result: Quote:
|
|||
22 May 2022, 13:35 |
|
FlierMate 23 May 2022, 11:27
I think it is "entry main". Or change the "main:" label to "entry $".
|
|||
23 May 2022, 11:27 |
|
< Last Thread | Next Thread > |
Forum Rules:
|
Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.
Website powered by rwasa.