Charles Pegge
08-03-2008, 21:37
In this example local variables are deployed on the stack. Note that the return address will be held
at [ebp-0] so variables must start at [ebp-4], assuming 4 byte variables, the next will be at [ebp-8]
and so on.
The variables are named VA VB and VC, whatever you choose.
' Using Machine Code with ThinBasic
'---------------------------------------------------------------------------
'---Reference:
' Intel x86 Architecture Ref Manual Vol 2
'---http://developer.intel.com/design/pentiumii/manuals/243191.htm
'---------------------------------------------------------------------------
' Syntax rules:
' #Variable patches in long address of Variable (4 bytes)
' NLn patches in long decimal value n (4 bytes)
' comments are indicated by a quote mark '
' all other words are read as hexadecimal bytes.
' An error during MC_Eval$ will produce a string containing &hc3 (ret) only.
'----------------------------------------------
' Allocating workspace on the stack for local use
'----------------------------------------------
dim sMC as string = MC_Eval$ "
55 ' push ebp ' important register
8b ec ' mov ebp,esp ' transfer stack pointer to ebp
'-------------------------------------------
81 ed NL100 ' sub esp,100 ' grab 100 bytes of stack space
'------------------------------------------
' do something with the space
b8 nl42 ' mov eax,42 ' load 42
89 45 fc ' mov [ebp-4],eax ' to variable VB
40 ' inc eax ' makes 43
89 45 f8 ' mov [ebp-8],eax ' to varianle VB
8b 4d fc ' mov ecx,[ebp-4] ' load VA into ecx
03 4d f8 ' add ecx,[ebp-8] ' add VB
89 4d f4 ' mov [ebp-12],ecx ' save in VC
8b 45 f4 ' mov eax,[ebp-12] ' load VC to eax
'-------------------------------------------
81 c5 NL100 ' add esp,100 ' release 100 bytes of stack
'-------------------------------------------
5d ' pop ebp ' restore ebp register
c3 ' ret ' return
"
'----------------------------------------------
' ---Invoke the machine code string
dim RetVal as long = MC_Exec(sMC)
MsgBox 0, "42 + 43 = " & RetVal
'
'----------------------------------------------
at [ebp-0] so variables must start at [ebp-4], assuming 4 byte variables, the next will be at [ebp-8]
and so on.
The variables are named VA VB and VC, whatever you choose.
' Using Machine Code with ThinBasic
'---------------------------------------------------------------------------
'---Reference:
' Intel x86 Architecture Ref Manual Vol 2
'---http://developer.intel.com/design/pentiumii/manuals/243191.htm
'---------------------------------------------------------------------------
' Syntax rules:
' #Variable patches in long address of Variable (4 bytes)
' NLn patches in long decimal value n (4 bytes)
' comments are indicated by a quote mark '
' all other words are read as hexadecimal bytes.
' An error during MC_Eval$ will produce a string containing &hc3 (ret) only.
'----------------------------------------------
' Allocating workspace on the stack for local use
'----------------------------------------------
dim sMC as string = MC_Eval$ "
55 ' push ebp ' important register
8b ec ' mov ebp,esp ' transfer stack pointer to ebp
'-------------------------------------------
81 ed NL100 ' sub esp,100 ' grab 100 bytes of stack space
'------------------------------------------
' do something with the space
b8 nl42 ' mov eax,42 ' load 42
89 45 fc ' mov [ebp-4],eax ' to variable VB
40 ' inc eax ' makes 43
89 45 f8 ' mov [ebp-8],eax ' to varianle VB
8b 4d fc ' mov ecx,[ebp-4] ' load VA into ecx
03 4d f8 ' add ecx,[ebp-8] ' add VB
89 4d f4 ' mov [ebp-12],ecx ' save in VC
8b 45 f4 ' mov eax,[ebp-12] ' load VC to eax
'-------------------------------------------
81 c5 NL100 ' add esp,100 ' release 100 bytes of stack
'-------------------------------------------
5d ' pop ebp ' restore ebp register
c3 ' ret ' return
"
'----------------------------------------------
' ---Invoke the machine code string
dim RetVal as long = MC_Exec(sMC)
MsgBox 0, "42 + 43 = " & RetVal
'
'----------------------------------------------