Author Topic: O2 Source ?  (Read 32597 times)

0 Members and 1 Guest are viewing this topic.

kryton9

  • Guest
Re: O2 Source ?
« Reply #15 on: July 26, 2011, 10:58:18 PM »
There are more than those Charle's if you start think about the windows, console and other api calls.

I wish I had a better understanding of the very low level stuff to help out. But I think finding a cross platform approach would be the way to go. As you see by your lines of code and as Eros has pointed how big thinBasic has gotten it just gets harder and harder later to port it over.

But what options does one have to make it cross platform, I don't know anymore. I have been researching this topic and the more I attempt to learn the more confused I get :)

Java and Mono are just in time compilers. I have seen via the Java Monkey Game Engine that you can compile to Windows, Linux and Mac. On windows you can make an executable file with the engine.
But via just Java, I have not been able to do that.

Mono, is .net but cross platform. I never thought the guys working on Mono would come as far as they have. Their IDE is even really nice.

The only one that is not a just in time compiler but a real compiler I see is free pascal. But again you need to find and use cross platform libraries.  gcc can be if only cross platform libraries are used and you are a wizard in writing make files, it is not easy. 

QT, I have not played with long enough to make a judgement.

What are your guys thoughts, what options does Charles have?


JRS

  • Guest
Re: O2 Source ?
« Reply #16 on: July 26, 2011, 11:00:06 PM »
Quote
I noticed that SB does not have MKL() and similar functions though these are mentioned as planned in the manual. You will have to construct binary encodings using asc() / chr().

A new user on the ScriptBasic forum reminded me that the s = t::ArrayToString(Array) T extension module function also works with LONG and REAL numbers. This should return in a string the binary (internal) value.

Code: [Select]
INCLUDE t.bas

a = 1
b = 1.0

sl_a = t::ArrayToString(a)
sr_b = t::ArrayToString(b)

PRINT HTA(sl_a),"\n"
PRINT HTA(sr_b),"\n"

END

FUNCTION HTA(arg)
  FOR x = 1 TO LEN(arg)
    z &= HEX(ASC(MID(arg,x,1)))
  NEXT
  HTA = z
END FUNCTION

I think it would be better to put my efforts into the O2 assembler and linker first and then work backwards. There is too much framework being defined for O2 that SB doesn't need to make the translation from O2 code to the pre-assembly state.
« Last Edit: July 26, 2011, 11:04:34 PM by JRS »

jcfuller

  • Guest
Re: O2 Source ?
« Reply #17 on: July 27, 2011, 02:18:57 AM »
Hi John,
Apart from these there is the more complex task of building an ELF header to replace the PE header. o2hdrs.bas deals with this aspect. I need to study some real ELF  headers to get a clear picture of how they are used. But it promises to be a lot cleaner than PE.

And in conjunction with this, resource building will also be totally different. An area I have not researched yet.

Charles



Charles,
  You might find some gems of information from the JAsm source?
http://www.japheth.de/JWasm.html
James
« Last Edit: July 27, 2011, 02:27:10 AM by jcfuller »

Charles Pegge

  • Guest
Re: O2 Source ?
« Reply #18 on: July 27, 2011, 08:58:28 AM »

Thanks James,

I love the list of "Hello World"s. This is very useful information.

Here is the one for Linux32

Code: OxygenBasic
  1. ;--- "hello world" for Linux which uses int 80h.
  2. ;--- assemble: jwasm -Fo=Linux1.o Linux1.asm
  3. ;--- link:     wlink format ELF runtime linux file Linux1.o name Linux1.
  4.  
  5.     .386
  6.     .model flat
  7.  
  8. stdout    equ 1
  9. SYS_EXIT  equ 1
  10. SYS_WRITE equ 4
  11.  
  12.     .data
  13.  
  14. string  db 10,"Hello, world!",10
  15.  
  16.     .code
  17.  
  18. _start:
  19.  
  20.     mov ecx, offset string
  21.     mov edx, sizeof string
  22.     mov ebx, stdout
  23.     mov eax, SYS_WRITE
  24.     int 80h
  25.     mov eax, SYS_EXIT
  26.     int 80h
  27.  
  28.     end _start
  29.  

Charles

Peter

  • Guest
Re: O2 Source ?
« Reply #19 on: July 27, 2011, 12:14:16 PM »
Here is my version.


Section .Data
msg      db "Hello World!"
msglen  equ 12

Section .Text
global  _start

_start:  mov edx,msglen
           mov ecx,msg
           mov ebx,1
           mov eax,4
           int 0x80
   
           mov ebx,0
           mov eax,1
           int 0x80

Charles Pegge

  • Guest
Re: O2 Source ?
« Reply #20 on: July 27, 2011, 05:17:49 PM »

This is how I would implement print in Oxygen's Linux run-time library:

Code: OxygenBasic
  1. function print cpu (string s)
  2.   asm
  3.   push ebx
  4.                   'string ptr in ecx
  5.  mov edx,[ecx-4] 'length
  6.  mov ebx,1       'stdout
  7.  mov eax,4       'SYS_WRITE
  8.  int 0x80
  9.   pop ebx
  10.   end asm
  11. end function
  12.  
  13. print "Hello World!"
  14.  

Charles

JRS

  • Guest
Re: O2 Source ?
« Reply #21 on: July 27, 2011, 06:14:03 PM »
Quote
This is how I would implement print in Oxygen's Linux run-time library:

Sounds like a commitment to Linux to me.  ;D

Charles Pegge

  • Guest
Re: O2 Source ?
« Reply #22 on: July 27, 2011, 06:38:14 PM »
Well certainly John, If Linux is that simple!


Kent
Quote
There are more than those Charles if you start think about the windows, console and other api calls.

Those 16 OS functions are the essentials required to run the Oxygen core in Windows. - bloated in my opinion :) but that is what is required in Windows. The list for Linux will be quite similar.

For the GUI I still advocate OpenGL as the best means of getting uniformity across the platforms and OpenGL code looks the same across many different languages. So it is easy to port.

Charles



JRS

  • Guest
Re: O2 Source ?
« Reply #23 on: July 27, 2011, 10:41:50 PM »
Charles,

I noticed ASM code in the assembler and linker. That means you have to be using external tools. Am I seeing old FreeBASIC code and not looking at the latest self compile version that you may not have released yet?

John

Charles Pegge

  • Guest
Re: O2 Source ?
« Reply #24 on: July 28, 2011, 12:54:12 AM »
John,

Those Asm routines you see in o2link are for converting decimal and hexadecimal numbers into binary and patching them directly into the binary output buffer t. They perform the equivalent first converting the text into a value then performing binary conversions chr(), mkshort(),mklong(), mkquad(), mksingle(), mkdouble() as required.

o2link is the first one to go for. It is the original o2 machine script encoder. Hardcore programmers who think that Assembler is a superfluous luxury will require nothing more  ;D


Charles

JRS

  • Guest
Re: O2 Source ?
« Reply #25 on: July 28, 2011, 12:54:35 AM »
Quote
Well certainly John, If Linux is that simple!

I figure if Wine can fake a Windows environment and run a wide array of software, (including O2) it must not make a lot of difference when your dealing directly with the CPU in an environment like ASM.

JRS

  • Guest
Re: O2 Source ?
« Reply #26 on: July 28, 2011, 01:11:35 AM »
Quote
o2link is the first one to go for.

Once I convert it to SB, can I test to see if it works? Can O2 provide the string I need to link?


Charles Pegge

  • Guest
Re: O2 Source ?
« Reply #27 on: July 28, 2011, 12:24:13 PM »
Hi John

One obstacle to doing this:

The Assembler produces a script which defines the memory image to be linked and o2link turns this into binary. But this memory image must then be compacted and post-processed to the PE file format.

This is done with the MakePE function in o2hdrs.bas.

Oxygen can emit a machine script for the memory image but not for the PE file itself.

This means you will need to port the MakePE function and its dependencies as well as o2link then feed the output of the linker into this function to generate the PE file.

I am refactoring makePE right now and will post an update soon.

Charles

Here is a machine script for "Hello World!"
Code: [Select]

`MZ`  90 00
03 00 00 00
04 00 00 00
FF FF 00 00
B8 00 00 00
00 00 00 00
40 00 00 00
$20
hle8
0E 1F BA 0E
00 B4 09 CD
21 B8 01 4C
CD 21
`This program cannot be run in DOS mode.` 0d 0a 00
/e8
`PE` 00 00
hw014c
hw0005
hl4E331D62
hl0
hl0
hw00e0
hw0102
hw010b 'MAGIC
02
00
hl0
hl0
hl00005000
ga _main_
ga _code_
 ga base_of_data
hg00400000
hl1000
hl200
hw4
hw0
hw1
hw0
hw4
hw0
hl0
hl5000
hl400
hl0
hw2
hw0
hg00100000
hg00001000
hg00100000
hg00001000
hl0
hl10
hl0 hl0
ga imports hl200
ga resources hl0
$68
`.text`    00 00 00 hl00000000  ga _code_    hl0000  $10   hl60000020
`.data`    00 00 00 hl40000000  ga _data     hl0200  $10   hlc0000040
`.bss`  00 00 00 00 hl00005000  ga bssdata   hl0000  $10   hlc0000080
`.idata`      00 00 hl02000000  ga imports   hl0200  $10   hlc0000040
`.rsrc`    00 00 00 hl02000000  ga resources hl0200  $10   hl40000040
/0ff0 e9 gf _o2_
/+1000
'  
._code_                         '  ._code_
._main_                         '  ._main_
 E8 gl get_bss                  '  call get_bss
 8B F8                          '  mov edi,eax
 E8 gl get_iat                  '  call get_iat
 8B D8                          '  mov ebx,eax
 89 9F 58 02 00 00              '  mov [edi+600],ebx
 6A 00                          '  push 0
 FF 53 0C                       '  call [ebx+12]
 89 87 60 02 00 00              '  mov [edi+608],eax
 9b db e3                       '  finit
 E8 gf get_oxygen               '  call fwd get_oxygen
 83 F8 00                       '  cmp eax,0
 0F 84 gf nend                  '  jz fwd nend
 FF D0                          '  call eax
 8B D8                          '  mov ebx,eax
 E8 gf _o2_                     '  call fwd _o2_
 E8 gl get_bss                  '  call get_bss
 8B F8                          '  mov edi,eax
 8B 9F 58 02 00 00              '  mov ebx,[edi+600]
 8B 87 68 02 00 00              '  mov eax,[edi+616]
 83 F8 00                       '  cmp eax,0
 0F 84 gf noxyfree              '  jz fwd noxyfree
 50                             '  push eax
 FF 53 08                       '  call [ebx+8]
.noxyfree                       '  .noxyfree
.nend                           '  .nend
 6A 00                          '  push 0
 8B C4                          '  mov eax,esp
 50                             '  push eax
 FF B7 60 02 00 00              '  push [edi+608]
 FF 53 14                       '  call [ebx+20]
 FF 53 18                       '  call [ebx+24]
.get_iat                        '  .get_iat
 E8 gf here                     '  call fwd here
.here                           '  .here
 58                             '  pop eax
 81 E8 ga here                  '  sub eax,here
 81 C0 ga import_address_table  '  add eax,import_address_table
 C3                             '  ret
.get_bss                        '  .get_bss
 E8 gf here                     '  call fwd here
.here                           '  .here
 58                             '  pop eax
 81 E8 ga here                  '  sub eax,here
 81 C0 ga bssdata               '  add eax,bssdata
 C3                             '  ret
.get_oxygen                     '  .get_oxygen
 57                             '  push edi
 68 66 67 00 00                 '  push 0x00006766
 68 65 6E 2E 63                 '  push 0x632e6e65
 68 6F 78 79 67                 '  push 0x6779786f
 8B C4                          '  mov eax,esp
 6A 00                          '  push 0
 68 80 00 00 00                 '  push 128
 6A 03                          '  push 3
 6A 00                          '  push 0
 6A 01                          '  push 1
 68 00 00 00 80                 '  push 0x80000000
 50                             '  push eax
 FF 53 1C                       '  call [ebx+28]
 83 C4 0C                       '  add esp,12
 83 F8 00                       '  cmp eax,0
 0F 84 gf nconfig               '  jz fwd nconfig
 8B F0                          '  mov esi,eax
 81 EC 04 08 00 00              '  sub esp,2052
 8B FC                          '  mov edi,esp
 C7 07 00 00 00 00              '  mov [edi],0
 6A 00                          '  push 0
 8B C4                          '  mov eax,esp
 6A 00                          '  push 0
 50                             '  push eax
 68 00 08 00 00                 '  push 2048
 57                             '  push edi
 56                             '  push esi
 FF 53 20                       '  call [ebx+32]
 56                             '  push esi
 FF 53 24                       '  call [ebx+36]
 8B CF                          '  mov ecx,edi
 FF C9                          '  dec ecx
 (                              '  (
 FF C1                          '  inc ecx
 8A 01                          '  mov al,[ecx]
 80 F8 20                       '  cmp al,32
 0F 87 rl                       '  ja repeat
 )                              '  )
 C7 01 00 00 00 00              '  mov [ecx],0
 57                             '  push edi
 FF 13                          '  call [ebx]
 81 C4 08 08 00 00              '  add esp,2056
 83 F8 00                       '  cmp eax,0
 0F 85 gf ndll                  '  jnz fwd ndll
.nconfig                        '  .nconfig
 68 6C 6C 00 00                 '  push 0x00006c6c
 68 65 6E 2E 64                 '  push 0x642e6e65
 68 6F 78 79 67                 '  push 0x6779786f
 8B C4                          '  mov eax,esp
 50                             '  push eax
 FF 13                          '  call [ebx]
 83 C4 0C                       '  add esp,12
 83 F8 00                       '  cmp eax,0
 0F 85 gf ndll                  '  jnz fwd ndll
 60                             '  pushad
 83 EC 20                       '  sub esp,32
 8B CC                          '  mov ecx,esp
 C7 01 4F 32 00 00              '  mov [ecx],0x324f
 C7 41 04 6F 78 79 67           '  mov [ecx+4],0x6779786f
 C7 41 08 65 6E 2E 64           '  mov [ecx+8],0x642e6e65
 C7 41 0C 6C 6C 3F 00           '  mov [ecx+12],0x003f6c6c
 6A 00                          '  push 0
 51                             '  push ecx
 83 C1 04                       '  add ecx,4
 51                             '  push ecx
 6A 00                          '  push 0
 FF 53 2C                       '  call [ebx+44]
 83 C4 20                       '  add esp,32
 61                             '  popad
 5F                             '  pop edi
 C3                             '  ret
.ndll                           '  .ndll
 5F                             '  pop edi
 89 87 68 02 00 00              '  mov [edi+616],eax
 8B D0                          '  mov edx,eax
 83 EC 10                       '  sub esp,16
 8B C4                          '  mov eax,esp
 C7 00 6F 32 5F 6C              '  mov [eax],0x6c5f326f
 C7 40 04 69 62 00 00           '  mov [eax+4],0x00006269
 50                             '  push eax
 52                             '  push edx
 FF 53 04                       '  call [ebx+04]
 83 C4 10                       '  add esp,16
 C3                             '  ret
._o2_                           '  ._o2_
 E9 gf _main                    '  jmp fwd _main
._mem                           '  ._mem
 E8 gl get_bss                  '  call get_bss
 8B D8                          '  mov ebx,eax
 C3                             '  ret
._newbuf                        '  ._newbuf
 68 00 10 00 00                 '  push 4096
 6A 00                          '  push 0  
 FF 93 A0 00 00 00              '  call [ebx+160]
 C7 C1 00 10 00 00              '  mov ecx,4096
 8B D0                          '  mov edx,eax
 (                              '  (
 C7 02 00 00 00 00              '  mov [edx],0
 83 C2 04                       '  add edx,4
 83 E9 04                       '  sub ecx,4
 0F 8F rl                       '  jg repeat
 )                              '  )
 89 07                          '  mov [edi],eax
 83 C7 08                       '  add edi,8
 C3                             '  ret
._main                          '  ._main
 55                             '  push ebp
 8B EC                          '  mov ebp,esp
 53                             '  push ebx
 56                             '  push esi
 57                             '  push edi
 8B FB                          '  mov edi,ebx
 E8 gl get_bss                  '  call get_bss
 8B D8                          '  mov ebx,eax
 8B F0                          '  mov esi,eax
 C7 C1 64 00 00 00              '  mov ecx,100
 (                              '  (
 8B 07                          '  mov eax,[edi]
 89 06                          '  mov [esi],eax
 83 C7 04                       '  add edi,4
 83 C6 04                       '  add esi,4
 FF C9                          '  dec ecx
 0F 8F rl                       '  jg repeat
 )                              '  )
 81 C7 6C 06 00 00              '  add edi,1644
 81 C6 6C 06 00 00              '  add esi, 1644
 C7 C1 00 02 00 00              '  mov ecx,512
 (                              '  (
 8B 07                          '  mov eax,[edi]
 89 06                          '  mov [esi],eax
 83 C7 04                       '  add edi,4
 83 C6 04                       '  add esi,4
 FF C9                          '  dec ecx
 0F 8F rl                       '  jg repeat
 )                              '  )
 8D BB 80 02 00 00              '  lea edi,[ebx+640]
 E8 gl _newbuf                  '  call _newbuf
 E8 gl _newbuf                  '  call _newbuf
 E8 gl _newbuf                  '  call _newbuf
 E8 gl _newbuf                  '  call _newbuf
 8D BB A8 02 00 00              '  lea edi,[ebx+680]
 E8 gl _newbuf                  '  call _newbuf
                                '  '_1
 8D 83 gc 1                     '  
 FF 93 68 08 00 00              '  call [ebx+2152]
 C6 C2 01                       '  mov dl,1
 FF 93 80 08 00 00              '  call [ebx+2176]
 C6 C2 01                       '  mov dl,1
 33 C0                          '  xor eax,eax
 FF 93 88 08 00 00              '  call [ebx+2184]
 50                             '  push eax
 C7 C0 01 00 00 00              '  mov eax,1
 FF 93 B0 08 00 00              '  call [ebx+2224]
 58                             '  pop eax
 FF 93 B0 08 00 00              '  call [ebx+2224]
 50                             '  push eax
 C6 C2 01                       '  mov dl,1
 FF 93 C0 09 00 00              '  call [ebx+2496]
 FF 93 38 08 00 00              '  call [ebx+2104]
                                '  '_2
 50                             '  push eax
 FF 93 90 09 00 00              '  call [ebx+2448]
 FF 93 60 08 00 00              '  call [ebx+2144]
 58                             '  pop eax
._end_                          '  ._end_
 5F                             '  pop edi
 5E                             '  pop esi
 5B                             '  pop ebx
 8B E5                          '  mov esp,ebp
 5D                             '  pop ebp
 C3                             '  ret
._error_                        '  ._error_
 51                             '  push ecx
 FF 93 00 08 00 00              '  call [ebx+2048]
 E9 gl _end_                    '  jmp _end_
.get_bss                        '  .get_bss
 E8 gf here                     '  call fwd here
.here                           '  .here
 58                             '  pop eax
 81 E8 ga here                  '  sub eax,here
 81 C0 ga bssdata               '  add eax,bssdata
 C3                             '  ret
 01 `end_of_code` 01 /+1000     '  
 
.base_of_data
._data
 gd 1 "Hello World!" 00 00

00 01 `end_of_data` 01 /+1000
.bssdata
$5000
.imports
ga name_list1 hl0 hl0 ga module_name1 ga address_list1
ga name_list3 hl0 hl0 ga module_name3 ga address_list3
hl0 hl0 hl0 hl0 hl0 ' termination
.module_name1 `KERNEL32.DLL` 00
.module_name3 `USER32.DLL` 00
.name_list1    gg _LoadLibrary gg _GetProcAddress gg _FreeLibrary
gg _GetModuleHandle gg _GetCommandLine gg _GetExitCodeProcess gg _ExitProcess
gg _CreateFile gg _ReadFile gg _CloseHandle hg0
.name_list3    gg _MessageBoxA gg _MessageBoxW hg0
/+8 .import_address_table
.address_list1    gg _LoadLibrary gg _GetProcAddress gg _FreeLibrary
gg _GetModuleHandle gg _GetCommandLine gg _GetExitCodeProcess gg _ExitProcess
gg _CreateFile gg _ReadFile gg _CloseHandle hg0
.address_list3    gg _MessageBoxA gg _MessageBoxW hg0
/+4 ._LoadLibrary        hw0 `LoadLibraryA` 00
/+4 ._GetProcAddress     hw0 `GetProcAddress` 00
/+4 ._FreeLibrary        hw0 `FreeLibrary` 00
/+4 ._GetModuleHandle    hw0 `GetModuleHandleA` 00
/+4 ._GetCommandLine     hw0  `GetCommandLineA` 00
/+4 ._GetExitCodeProcess hw0 `GetExitCodeProcess` 00
/+4 ._ExitProcess        hw0 `ExitProcess` 00
/+4 ._CreateFile         hw0 `CreateFileA` 00
/+4 ._ReadFile           hw0 `ReadFile` 00
/+4 ._CloseHandle        hw0 `CloseHandle` 00
/+4 ._MessageBoxA hw0 `MessageBoxA` 00
/+4 ._MessageBoxW hw0 `MessageBoxW` 00
01 `end_of_imports` 01
/+1000
.resources
hl0 hl0 hl1 hw0 hw1
hl6 hl80000018
hl0 hl0 hl1 hw0 hw1
hl1 hl80000030
hl0 hl0 hl1 hw0 hw1
nl2057 hl48
ga data1 nl32 nl0 hl0
.data1 nw11 w`OxygenBasic` nw3 w`o2h` nw0
/+200
01 `end_of_resources` 01
/+1000

« Last Edit: July 29, 2011, 01:19:32 AM by Charles Pegge »

Aurel

  • Guest
Re: O2 Source ?
« Reply #28 on: July 28, 2011, 01:04:05 PM »
Quote
Oxygen can emit a machine script for the memory image but not for the PE file itself.
Im not expert but i see that old LibryCompiler written in VB 6.0 can create directly PE file.

JRS

  • Guest
Re: O2 Source ?
« Reply #29 on: July 28, 2011, 01:17:44 PM »
Quote
Here is a machine script for "Hello World!"

WOW!

That has to be the most complex 'Hello World" I have ever seen.  :o