Machine code question

Started by Frank Brübach, January 14, 2025, 01:58:55 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Frank Brübach

hello charles, found this old example with machine code, can you please correct this first code example? thanks, frank

' machine code test o2, frank bruebach
'
    '--------------'
    ' nested loops
    '--------------'
    ' machine code, old example

' 1)
'------------------------------- //
o2
(
    b9 nl5
    33 d2
    (
        b8 nl10
        (
            42
            48 7f repeat
        )
        49
        7f repeat
    )
    eb gEnd ' jump over some string data
    'eb gEnd ' error message here
    [ this is a string ]

    .End_of_Prog

    8b c2
    c3
)

' 2)
'--------------------- //
' go below
'
o2
(
 b8 00 00 00 00
 b9 10 00 00 00
 (
  40
  49
  74 x
  eb r
 )
 e9 gf ok
)
print "A " eax

o2 .ok

print "B " eax


Frank Brübach


second question:

how do you have found this machine code output for the below function f() ?


function f(sys a,b) as sys
return a+b
end function
 
' how do you find this code output for the above function?
'
'o2 machine script:
'Code: OxygenBasic
                                '  '_4
                                '  'FUNCTION F
 E9 gf _over_                   '  jmp fwd _over_
!10
.f                              '  .f
.f#sys#sys                      '  .f#sys#sys
 (                              '  (
 53                             '  push ebx
 56                             '  push esi
 57                             '  push edi
 50                             '  push eax
 E8 gl _mem                     '  call _mem
 55                             '  push ebp
 8B EC                          '  mov ebp,esp
 83 C4 F0                       '  add esp,-16
 8D 7D F0                       '  lea edi,[ebp-0x10]
 C7 07 00 00 00 00              '  mov [edi],0
 8B 45 18                       '  mov eax,[ebp+0x18]
 03 45 1C                       '  add eax,[ebp+0x1C]
 E9 gf _return_                 '  jmp fwd _return_
                                '  '_6
._exit_                         '  ._exit_
 8B 45 F0                       '  mov eax,[ebp-0x10]
._return_                       '  ._return_
 8B E5                          '  mov esp,ebp
 5D                             '  pop ebp
 83 C4 04                       '  add esp,4
 5F                             '  pop edi
 5E                             '  pop esi
 5B                             '  pop ebx
 C2 08 00                       '  ret 8
 )                              '  )
._over_                         '  ._over_
                                '  '_7


Charles Pegge

Hi Frank,

If you want to hack some o2 machine script (lowest level before linking)

write some simple code and compile it in the command console:

co2 -m -b mycode

example:

source
int a
a=33
while a>0
  a--
wend

listing:
._code_                         '  ._code_
._main_                         '  ._main_
 E9 gf _initjit                 '  jmp fwd _initjit
                                '  'UTILITIES:
                                '  '
                                '  'GET ADDRESS OF BSSDATA, STORE IN RBX
                                '  '====================================
._mem                           '  ._mem
 E8 gf _h_                      '  call fwd _h_
._h_                            '  ._h_
 5B                             '  pop rbx
 81 EB ga _h_                   '  sub rbx,_h_
 81 C3 ga bssdata               '  add rbx,bssdata
 C3                             '  ret
                                '  '
                                '  '
                                '  'MAIN ENTRY POINT
                                '  '================
._initjit                       '  ._initjit
 55                             '  push rbp :
 8B EC                          '  mov rbp,rsp
                                '  'call CopyLib
                                '  'SPACE FOR GLOBAL BUFFER POINTERS
 6A 00                          '  push 0
 6A 00                          '  push 0
 6A 00                          '  push 0
 6A 00                          '  push 0
 68 00 02 00 00                 '  push 512  :
 FF 53 08                       '  call [rbx+8] :
 89 83 98 02 00 00              '  mov [rbx+664],rax 'BUFFER FOR GLOBAL STRINGS 664
 68 00 10 00 00                 '  push 4096 :
 FF 53 08                       '  call [rbx+8] :
 89 83 A8 02 00 00              '  mov [rbx+680],rax 'BUFFER FOR LIB HANDLES 680
==============================
'PROG STARTS HERE
==============================
'_1
'_2
 C7 83 04 20 00 00 21 00 00 00  '  mov dword [ebx+8196],33
'_3
._do1                           '  ._do1
 83 BB 04 20 00 00 00           '  cmp dword [ebx+8196],0
 0F 8F gf _end_cnd1             '  jg   fwd _end_cnd1
 E9 gf _end_do1                 '  jmp fwd _end_do1
._end_cnd1                      '  ._end_cnd1
._end_if1                       '  ._end_if1
'_4
 FF 8B 04 20 00 00              '  dec dword [ebx+8196]
'_5
 E9 gl _do1                     '  jmp long _do1
._end_do1                       '  ._end_do1
._end_prog                      '  ._end_prog
==============================================

 FF 93 90 09 00 00              '  call  [ebx+2448]
 FF 93 60 08 00 00              '  call  [ebx+2144]
._end_                          '  ._end_
 8B E5                          '  mov rsp,rbp :
 5D                             '  pop rbp
 C3                             '  ret
._error_                        '  ._error_
 (                              '  (
 e8 hl0
 58                             '  pop eax
 83 C0 09                       '  add eax,9
 e9 xl                          '  exit
 `MISSING OR UNLOADABLE` 00 00
 )                              '  )
 6A 30                          '  push 0x30
 50                             '  push eax
 51                             '  push ecx
 6A 00                          '  push 0
 FF 93 D8 01 00 00              '  call [ebx+472]
 E9 gl _end_                    '  jmp _end_

/+1000
._data_

00 01 `end_of_data` 01 /+1000

o2 p3
.bssdata
$3000


End_of_Prog



Frank Brübach

#3
thanks charles, my machine code example <1myexample.o2bas> works, great and produces a long list :-)
'
but how I can save this console file and get a text file for saving and display the result?

'
' 1myexample.o2bas
'
int a,b
a=2025
b=350
print "all days of this year "+a*b

'' co2 -m -b 1myexample.o2bas

And whats the next step to Set and build a linker ?

Frank Brübach

I have the solution, all ok here :-)


using -> command prompt

co2 -m -b 1myexample.o2bas
string s="1myexample.o2bas"
print getfile s
print s

'
' -> co2.txt file produces all :-)
'

._code_                         '  ._code_
._main_                         '  ._main_
 E9 gf _initjit                 '  jmp fwd _initjit
                                '  'UTILITIES:
                                '  '
                                '  'GET ADDRESS OF BSSDATA, STORE IN RBX
                                '  '====================================
._mem                           '  ._mem
 E8 gf _h_                      '  call fwd _h_
._h_                            '  ._h_
 5B                             '  pop rbx
 81 EB ga _h_                   '  sub rbx,_h_
 81 C3 ga bssdata               '  add rbx,bssdata
 C3                             '  ret
                                '  '
                                '  '
                                '  'MAIN ENTRY POINT
                                '  '================
._initjit                       '  ._initjit
 55                             '  push rbp :
 8B EC                          '  mov rbp,rsp
                                '  'call CopyLib
                                '  'SPACE FOR GLOBAL BUFFER POINTERS
 6A 00                          '  push 0
 6A 00                          '  push 0
 6A 00                          '  push 0
 6A 00                          '  push 0
 68 00 02 00 00                 '  push 512  :
 FF 53 08                       '  call [rbx+8] :
 89 83 98 02 00 00              '  mov [rbx+664],rax 'BUFFER FOR GLOBAL STRINGS 664
 68 00 10 00 00                 '  push 4096 :
 FF 53 08                       '  call [rbx+8] :
 89 83 A8 02 00 00              '  mov [rbx+680],rax 'BUFFER FOR LIB HANDLES 680
'_1
'_2
'_3
'_4
'_5
 C7 83 04 20 00 00 E9 07 00 00  '  mov dword [ebx+8196],2025
'_6
 C7 83 08 20 00 00 5E 01 00 00  '  mov dword [ebx+8200],350
'_7
 8D 83 gc 1
 8B C0                          '  mov eax,eax
 FF 93 68 08 00 00              '  call [ebx+2152] 'join prep
 C6 C2 01                       '  mov dl,1
 FF 93 78 08 00 00              '  call [ebx+2168] 'add bstring
 8B 83 04 20 00 00              '  mov eax,[ebx+8196]
 F7 AB 08 20 00 00              '  imul dword [ebx+8200]
                                '  'CONVERT ;CONVERT VAL TO STRING
                                '  'CONVERT CPU TO FPU
 50                             '  push eax
 DB 04 24                       '  fild dword [esp]
 83 C4 04                       '  add esp,4
 FF 93 D8 09 00 00              '  call [ebx+2520] 'str
 FF 93 B0 08 00 00              '  call [ebx+2224]
 8B C0                          '  mov eax,eax
 C6 C2 01                       '  mov dl,1
 FF 93 78 08 00 00              '  call [ebx+2168] 'add bstring
 C6 C2 01                       '  mov dl,1
 33 C0                          '  xor eax,eax
 FF 93 88 08 00 00              '  call [ebx+2184] 'join
 FF 93 B0 08 00 00              '  call [ebx+2224]
 89 83 0C 20 00 00              '  mov [ebx+8204],eax
 8B 83 0C 20 00 00              '  mov eax,[ebx+8204]
 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]
._end_prog                      '  ._end_prog
 FF 93 90 09 00 00              '  call  [ebx+2448]
 FF 93 60 08 00 00              '  call  [ebx+2144]
._end_                          '  ._end_
 8B E5                          '  mov rsp,rbp :
 5D                             '  pop rbp
 C3                             '  ret
._error_                        '  ._error_
 (                              '  (
 e8 hl0
 58                             '  pop eax
 83 C0 09                       '  add eax,9
 e9 xl                          '  exit
 `MISSING OR UNLOADABLE` 00 00
 )                              '  )
 6A 30                          '  push 0x30
 50                             '  push eax
 51                             '  push ecx
 6A 00                          '  push 0
 FF 93 D8 01 00 00              '  call [ebx+472]
 E9 gl _end_                    '  jmp _end_
 
/+1000
._data_
/+4 gd 1 16 00 00 00 "all days of this year " hw0

00 01 `end_of_data` 01 /+1000

o2 p3
.bssdata
$3000




1myexample.o2bas

COMPILATION DATA:

output file name:
output file length: 0

source files: 1
source lines: 7
source word parsing: 27
source length: 88
assembler script length: 1487
machine script length: 4261

record size: 36
static record count: 464
static records length: 16704
static record string space: 13778
max record string space: 15607
static var space: 8208
procedures: 0
source buffer insert patches: 4
source buffer insert renew: 1
max link labels: 0
binary image size: 0


Charles Pegge

#5
Yes, to make a text file do this: (dos > output to file)

co2 -m -b mycode >t.txt


How to make a linker?

inc\self\asm.inc converts asm to machine script

inc\self\link.inc links and converts machine script to binary

inc\self\main.inc is the top level of the compiler and shows you how it is put together.

Frank Brübach

Yes thanks thats working OK with File saving..

co2 -m -b 1myexample.o2bas >1ft.txt

With complete content / output of machine code

Frank Brübach

Hello Charles... Short question Same topic... Can I also reverse the entire machine code process up to the function or script at the beginning?
Nice day

Charles Pegge

No. Symbolic information is mostly lost during compilation, The machine is only concerned with cpu instructions and locations.

Zlatko Vid

just a observation:
i hope that is not stupid  ;D

   what i understand is comment(i hope)  :D
        jmp fwd _initjit
        call fwd _h_

anything else ..ouch to much for my brain  ;D

Charles Pegge

#10
fwd tells the compiler to look ahead for the next matching label instead of looking backward first.

you can also specify goto next xyz or goto prev xyz. This is also helpful when reading code.