LocoDelAssembly
Your code has a bug
Joined: 06 May 2005
Posts: 4624
Location: Argentina
LocoDelAssembly 27 May 2007, 04:14
Yes, its pretty hard. Here my try
; r0: Always contains 0 ; r1: Reserved for assembler use (obviously fasmMIPS doesn't reserve it because it ; doesn't have built-in macroinstruction such as LI) macro li reg, imm { if ~reg eqtype r0 display "I said a register, BITCH!", 13, 10 err else if reg eq r1 display "Destination register is reserved for LI", 13, 10 err else if imm >= 1 shl 32 display "Immediate out of range", 13, 10 err end if if (imm >= -1 shl 15) & (imm < 1 shl 15) addiu reg, r0, imm else lui reg, imm shr 16 and $FFFF ori reg, imm and $FFFF end if } macro li64 reg, imm { if ~reg eqtype r0 display "I said a register, BITCH!", 13, 10 err else if reg eq r1 display "Destination register is reserved for LI", 13, 10 err end if if imm >= 0 if imm < 1 shl 15 addiu reg, r0, imm else if imm < 1 shl 31 lui reg, imm shr 16 and $FFFF ori reg, imm and $FFFF end if else lui r1, imm shr 48 and $FFFF lui reg, imm shr 16 and $FFFF ori r1, r1, imm shr 32 and $FFFF ori reg, reg, imm and $FFFF ; WARNING: The processor must be properly initialized to prevent Reserved Instruction exception. ; Obviously it must be a MIPS64 aswell... dsll32 reg, reg, 0 dsll32 r1, r1, 0 dsrl32 reg, reg, 0 or reg, r1, reg end if }
Note that I’m not a MIPS assembly programmer so possibly this is not the best way.
[EDIT] I corrected a sign extention related bug and improved the macros a little. I found the li64 ridiculously complex, any suggestion is welcomed.[/EDIT]
-
djmans
New Member
- Публикаций:
-
0
- Регистрация:
- 27 дек 2006
- Сообщения:
- 312
С фасмом плохо знаком, подскажите как писать call такого типа.
call qword [0x1FFFFFFFFFFFFFFF]
почемуто ругается value out of range.
тема
mov rax, 0x1FFFFFFFFFFFFFFF
call qword [rax]не устраивает
-
Mika0x65
New Member
- Публикаций:
-
0
- Регистрация:
- 30 июл 2005
- Сообщения:
- 1.384
Никак. Адресации типа [disp64] нет. Либо пользоваться 32битным значением, либо ‘mov eax, [64bit_addr_of_memory]; call eax’.
-
djmans
New Member
- Публикаций:
-
0
- Регистрация:
- 27 дек 2006
- Сообщения:
- 312
-
srvaldez
- Posts: 3106
- Joined: Sep 25, 2005 21:54
need help with FasmDLL
I am trying out the fasm DLL found here http://board.flatassembler.net/topic.php?t=6239
but can’t get it to work, the stack seems to get messed up, hope you can help.
Code: Select all
#Include "Windows.Bi"
#Include "Crt.Bi"
' the following structure resides at the beginning of memory block provided
' to the fasm_assemble function. the condition field contains the same value
' as the one returned by function.
' when function returns fasm_ok condition, the output_length and
' output_data fields are filled - with pointer to generated output
' (somewhere within the provided memory block) and the count of bytes stored
' there.
' when function returns fasm_error, the error_code is filled with the
' code of specific error that happened and error_line is a pointer to the
' line_header structure, providing information about the line that caused
' the error.
Type Fasm_State
As Integer Condition
Union
As Integer Output_Length
As Integer Error_Code
End Union
Union
As Any Ptr Output_Data
As Any Ptr Error_Line
End Union
End Type
' the following structure has two variants - it either defines the line
' that was loaded directly from source, or the line that was generated by
' macroinstruction. first case has the highest bit of line_number set to 0,
' while the second case has this bit set.
' in the first case, the file_path field contains pointer to the path of
' source file (empty string if it's the source that was provided directly to
' fasm_assemble function), the line_number is the number of line within
' that file (starting from 1) and the file_offset field contains the offset
' within the file where the line starts.
' in the second case the macro_calling_line field contains the pointer to
' line_header structure for the line which called the macroinstruction, and
' the macro_line field contains the pointer to line_header structure for the
' line within the definition of macroinstruction, which generated this one.
Type Line_Header
As Integer File_Path
As Integer Line_Number
Union
As Integer File_Offset
As Integer Macro_Calling_Line
End Union
As Integer Macro_Line
End Type
' General Errors And Conditions
Function Fasm_Error(Byval Error_Code As Integer) As String
Select Case Error_Code
Case 0
Return "Fasm Ok"
Case 1
Return "Fasm Working"
Case 2
Return "Fasm Error"
Case -1
Return "Fasm Invalid Parameter"
Case -2
Return "Fasm Out Of Memory"
Case -3
Return "Fasm Stack Overflow"
Case -4
Return "Fasm Source Not Found"
Case -5
Return "Fasm Unexpected End Of Source"
Case -6
Return "Fasm Cannot Generate Code"
Case -7
Return "Fasm Format Limitations Excedded"
Case -8
Return "Fasm Write Failed"
' Error Codes For Fasm Error Condition
Case -101
Return "Fasm error: File Not Found"
Case -102
Return "Fasm error: Error Reading File"
Case -103
Return "Fasm error: Invalid File Format"
Case -104
Return "Fasm error: Invalid Macro Arguments"
Case -105
Return "Fasm error: Incomplete Macro"
Case -106
Return "Fasm error: Unexpected Characters"
Case -107
Return "Fasm error: Invalid Argument"
Case -108
Return "Fasm error: Illegal Instruction"
Case -109
Return "Fasm error: Invalid Operand"
Case -110
Return "Fasm error: Invalid Operand Size"
Case -111
Return "Fasm error: Operand Size Not Specified"
Case -112
Return "Fasm error: Operand Sizes Do Not Match"
Case -113
Return "Fasm error: Invalid Address Size"
Case -114
Return "Fasm error: Address Sizes Do Not Agree"
Case -115
Return "Fasm error: Disallowed Combination Of Registers"
Case -116
Return "Fasm error: Long Immediate Not Encodable"
Case -117
Return "Fasm error: Relative Jump Out Of Range"
Case -118
Return "Fasm error: Invalid Expression"
Case -119
Return "Fasm error: Invalid Address"
Case -120
Return "Fasm error: Invalid Value"
Case -121
Return "Fasm error: Value Out Of Range"
Case -122
Return "Fasm error: Undefined Symbol"
Case -123
Return "Fasm error: Invalid Use Of Symbol"
Case -124
Return "Fasm error: Name Too Long"
Case -125
Return "Fasm error: Invalid Name"
Case -126
Return "Fasm error: Reserved Word Used As Symbol"
Case -127
Return "Fasm error: Symbol Already Defined"
Case -128
Return "Fasm error: Missing End Quote"
Case -129
Return "Fasm error: Missing End Directive"
Case -130
Return "Fasm error: Unexpected Instruction"
Case -131
Return "Fasm error: Extra Characters On Line"
Case -132
Return "Fasm error: Section Not Aligned Enough"
Case -133
Return "Fasm error: Setting Already Specified"
Case -134
Return "Fasm error: Data Already Defined"
Case -135
Return "Fasm error: Too Many Repeats"
Case -136
Return "Fasm error: Symbol Out Of Scope"
Case -140
Return "Fasm error: User Error"
Case -141
Return "Fasm error: Assertion Failed"
End Select
End Function
#Define Lf Chr(10)
#Define Cr Chr(13)
Dim Fasm_Getversion As Function Stdcall () As Integer
Dim Fasm_Assemble As Function Stdcall _
(Byval Lpsource As Any Ptr, _
Byval Lpmemory As Any Ptr, _
Byval Cbmemorysize As Integer, _
Byval Npasslimit As Integer, _
Byval Hdisplaypipe As Any Ptr ) As Integer
Dim Fasm_Assemblefile As Function Stdcall _
(Byval Lpsourcefile As Any Ptr, _
Byval Lpmemory As Any Ptr, _
Byval Cbmemorysize As Integer, _
Byval Npasslimit As Integer, _
Byval Hdisplaypipe As Any Ptr ) As Integer
Dim As Any Ptr Library
Library = Dylibload( "C:Program FilesFreeBASICexamplesFasm" )
If( Library = 0 ) Then
Print "Cannot Load The Fasm Dynamic Library, Aborting Program..."
Print "Press Return To End ";
Sleep
Else
Fasm_Getversion = Dylibsymbol( Library, "fasm_GetVersion")
If Fasm_Getversion = 0 Then
Print "Cannot Get Fasm_Getversion Function Address From Fasm Library, Aborting Program..."
Print "Press Return To End ";
Sleep
End 1
End If
Fasm_Assemble = Dylibsymbol( Library, "fasm_Assemble")
If Fasm_Assemble = 0 Then
Print "Cannot Get Fasm_Assemble Function Address From Fasm Library, Aborting Program..."
Print "Press Return To End ";
Sleep
End 1
End If
Fasm_Assemblefile = Dylibsymbol( Library, "fasm_AssembleFile")
If Fasm_Assemblefile = 0 Then
Print "Cannot Get Fasm_Assemblefile Function Address From Fasm Library, Aborting Program..."
Print "Press Return To End ";
Sleep
End 1
End If
End If
Dim Asm_Source As Zstring Ptr = Allocate(1024)
Dim Buffer As Fasm_State Ptr = Allocate(8388608)
Dim Line_Error As Line_Header Ptr
Dim Machine_Code As Any Ptr
Dim As Integer Result
Dim As Integer I
Dim Myfunction As Function Cdecl() As Integer
*Asm_Source="use32"+Lf
*Asm_Source+="Org 100H"+Lf
*Asm_Source+=Lf
*Asm_Source+="Mov Eax,100"+Lf
*Asm_Source+="Add Eax,20"+Lf
'*Asm_Source+="Fldpi"+Lf 'If This Line Is Uncommented And The Return Type
*Asm_Source+="Ret"+Lf 'Of Myfunction Is Changed To Double Then It Returns Pi
Result=Fasm_Assemble(Asm_Source, Buffer,8388608,100,0)
Print "Buffer->Condition = ";Buffer->Condition
If Buffer->Condition=0 Then
Print "Buffer->Output_Length = ";Buffer->Output_Length
End If
If Result=0 Then
Machine_Code=Virtualalloc(Null, Buffer->Output_Length, Mem_Commit, Page_Execute_Readwrite)
Memcpy(Machine_Code, Buffer->Output_Data, Buffer->Output_Length)
Print
Print "Asm Source"
Print *Asm_Source
Print "Machine Code ";
For I=0 To Buffer->Output_Length-1
Print Hex(Peek(Ubyte,Machine_Code+I),2);" ";
Next
Print
Print
Myfunction=Machine_Code
Print "Result Of Myfunction = ";
Print Myfunction()
End If
If Buffer->Condition=2 Then
Line_Error=Buffer->Error_Line
Print Fasm_Error(Buffer->Error_Code);" In Line ";Line_Error->Line_Number
End If
'Another Way Of Calling The Asm Code
I=0
If Buffer->Condition=0 Then
Asm
Call [Machine_Code]
Mov [I],Eax
End Asm
End If
Print "Result From Calling Machine Code = ";I
Deallocate(Buffer)
Deallocate(Asm_Source)
Virtualfree(Machine_Code,0,Mem_Release)
Dylibfree(Library)
Print "Press Return To End ";
Sleep
Last edited by srvaldez on Sep 06, 2012 4:38, edited 4 times in total.
-
Gonzo
- Posts: 722
- Joined: Dec 11, 2005 22:46
Re: need help with FasmDLL
Post
by Gonzo » Sep 05, 2012 21:26
did you try cdecl instead of stdcall ? it doesnt say anything in the small documentation
wrong calling conventions tend to break the stack, same if you provide a different number of arguments than what is expected
-
srvaldez
- Posts: 3106
- Joined: Sep 25, 2005 21:54
Re: need help with FasmDLL
Post
by srvaldez » Sep 05, 2012 21:37
changing the fasm functions to cdecl only makes it worse an changing myFunction to cdecl makes no difference,
I can understand why print myFunction() would not work as I expect but this should work
Code: Select all
i=0
if buffer->condition=0 then
asm
call [machine_code]
mov [i],eax
end asm
end if
print i
-
MichaelW
- Posts: 3500
- Joined: May 16, 2006 22:34
- Location: USA
Re: need help with FasmDLL
Post
by MichaelW » Sep 06, 2012 1:36
The provided example and your source both assemble 16-bit code, which generally is not be callable from 32-bit code. There may be a way to direct FASM to assemble 32-bit code, but I have no idea how to do that.