|
Post by vovchik on Dec 20, 2014 10:58:48 GMT 1
Dear all,
Since the BASM thread is closed, I am reposting the first post from that thread that dealt with BaCon and assembler... Perhaps we could post our contributions here.
Here is a little working example:
' declare some integers to use in assembly DECLARE foo=20, bar=50 TYPE int PRINT "foo = ", foo PRINT "bar = ", bar ' prep defines assembly to Basic type USEC #define ASM asm( #define GAS ); END USEC ' add two numbers foo and bar USEC ASM "addl %%ebx,%%eax" :"=a"(foo) :"a"(foo), "b"(bar) GAS END USEC ' Show vars and result of ASM additon PRINT "ASM routine foo + bar = ", foo PRINT "Brought to you courtesy of BaCon ASM. :)"
Or do it in a SUB:
' declare some integers to use in assembly DECLARE foo=20, bar=50 TYPE int ' prep defines assembly to Basic type
SUB ASM_ADD() ' add two numbers foo and bar USEC #define ASM asm( #define GAS ); ASM "addl %%ebx,%%eax" :"=a"(foo) :"a"(foo), "b"(bar) GAS END USEC END SUB
PRINT "foo = ", foo PRINT "bar = ", bar ASM_ADD() ' Show vars and result of ASM additon PRINT "ASM routine foo + bar = ", foo PRINT "Brought to you courtesy of BaCon ASM."
Or do it as a function:
DECLARE foo1=50, bar1=20 TYPE int
FUNCTION ASM_ADD(int foo, int bar) ' add two numbers foo and bar USEC ' prep defines assembly to Basic type #define ASM asm( #define GAS ); ASM "addl %%ebx,%%eax" :"=a"(foo) :"a"(foo), "b"(bar) GAS END USEC RETURN foo END FUNCTION
PRINT "foo = ", foo1 PRINT "bar = ", bar1 ' Show vars and result of ASM additon PRINT "ASM routine foo + bar = ", ASM_ADD(foo1, bar1) PRINT "Brought to you courtesy of BaCon ASM."
With kind regards, vovchik
|
|
|
Post by alexfish on Dec 20, 2014 14:57:16 GMT 1
Hi Vovchik Looks good. noted from Examples : using extended Asm extract from inline-asmso thought would put this one to a test .. as in mixing c and asm. have modified the original example into a function less the rand so hopeful one can Picture of what is going on. Here We Go , play with the bits BR Alex FUNCTION TEST(int operand1,int operand2) TYPE int
USEC #define ASM asm( #define GAS );
int sum, accumulator;
ASM "movl %1, %0\n\t" "addl %2, %0" : "=r" (sum) /* output operands */ : "r" (operand1), "r" (operand2) /* input operands */ : "0" /* clobbered operands */ GAS accumulator = sum;
ASM "addl %1, %0\n\t" "addl %2, %0" : "=r" (accumulator) : "0" (accumulator), "g" (operand1), "r" (operand2) : "0" GAS
ENDUSEC
RETURN accumulator
END FUNCTION
REM main
my_accumulator=TEST(1,0) PRINT my_accumulator
my_accumulator=TEST(1,1) PRINT my_accumulator
my_accumulator=TEST(1,2) PRINT my_accumulator
my_accumulator=TEST(1,3) PRINT my_accumulator
|
|
|
Post by alexfish on Dec 20, 2014 15:29:02 GMT 1
Hi all
Mix some bits here test integer to double
DECLARE my_accumulator TYPE double
FUNCTION TEST(int operand1,int operand2) TYPE double
USEC #define ASM asm( #define GAS );
int sum, accumulator;
ASM "movl %1, %0\n\t" "addl %2, %0" : "=r" (sum) /* output operands */ : "r" (operand1), "r" (operand2) /* input operands */ : "0" /* clobbered operands */ GAS accumulator = sum;
ASM "addl %1, %0\n\t" "addl %2, %0" : "=r" (accumulator) : "0" (accumulator), "g" (operand1), "r" (operand2) : "0" GAS
ENDUSEC
RETURN (double)accumulator
END FUNCTION
REM main
my_accumulator=TEST(1,0) PRINT my_accumulator
my_accumulator=TEST(1,1) PRINT my_accumulator
my_accumulator=TEST(1,2) PRINT my_accumulator
my_accumulator=TEST(1,3) PRINT my_accumulator REM does it work my_accumulator=my_accumulator + 1.3 PRINT my_accumulator
Restest/
DECLARE my_accumulator TYPE double DECLARE oper_1 TYPE double
FUNCTION TEST(int operand1,int operand2) TYPE double
USEC #define ASM asm( #define GAS );
int sum, accumulator;
ASM "movl %1, %0\n\t" "addl %2, %0" : "=r" (sum) /* output operands */ : "r" (operand1), "r" (operand2) /* input operands */ : "0" /* clobbered operands */ GAS accumulator = sum;
ASM "addl %1, %0\n\t" "addl %2, %0" : "=r" (accumulator) : "0" (accumulator), "g" (operand1), "r" (operand2) : "0" GAS
ENDUSEC
RETURN (double)accumulator
END FUNCTION
REM main
oper_1 = 1.8 my_accumulator=TEST(oper_1,4) PRINT my_accumulator
my_accumulator=my_accumulator + 1.3 PRINT my_accumulator
|
|
|
Post by Pjot on Dec 21, 2014 15:23:02 GMT 1
Thanks guys, Interesting stuff. My experience with assembly mainly related to the good old 6502/6510 CPU. In the 8086 I did something with assembly as well but it's a long time ago... so I'm afraid I cannot be of much help here... Regards Peter
|
|
|
Post by greco558 on Dec 30, 2014 2:56:50 GMT 1
Hi All, Here is my demo vaultgui.bac program that started by using a shared library that contained C and Assembly code. I then changed it to compile and link all the separate bacon, c, and assembly files into a single binary to eliminate the need of having a shared library. Now here is the vaultgui.bac renamed vaultasm.bac that inlines all C and Assembly code into our BaCon file. Had to add a couple extra lines in assembly code because I write in intel syntax not at&t syntax. You will need to use the makeall.sh script to compile because I had to add link option -masm=intel.. BR greco558 Attachments:vaultasm.tar (30 KB)
|
|
Deleted
Deleted Member
Posts: 0
|
Post by Deleted on Dec 30, 2014 3:21:09 GMT 1
I'm glad to see you guys are still interested in ASM and BASIC. I hope there is no hard feelings about closing the BASM thread. I felt it was taking too much attention away from BaCon which is what this forum is all about.
The invitation is still open if you have any spare time to help out with the BASM project. All contributions large or small are appreciated.
Thanks for the effort you have made so far!
|
|
|
Post by vovchik on Dec 30, 2014 19:38:31 GMT 1
Dear greco558,
Very nicely done...thanks. It is also a good example for demonstrating BaCon's easly accommodation of C and ASM.
With kind regards, vovchik
|
|
|
Post by alexfish on Dec 30, 2014 20:29:23 GMT 1
Hi greco558
Very nice :: esp the use of the Intel switch using Bacon With Inline assembly
example from the vaultasm.bac
USEC ASM ".intel_syntax noprefix;" "mov ebx,16;" "dec esi;" "dec edi;" ".decrypt:;" "push ecx;" "mov ecx,ebx;" "mov al,byte [edi+ebx-1];" "xor byte[esi],al;" "ror byte[esi],cl;" "inc esi;" "dec ebx;" "cmp ebx,0;" "jne .notzero;" "mov ebx,16;" ".notzero:;" "pop ecx;" "loop .decrypt;" ".att_syntax;" : :"c"(filesize), "S"(buffer), "D"(fullkey) :"eax", "ebx" INTEL END USEC
+ Compiled out of the box with Bacon Gui
BR Alex
|
|
|
Post by greco558 on Dec 30, 2014 22:23:24 GMT 1
Thanks vovchik, Alex , I am still amazed at the things you can do with BaCon. Here is an updated vaultasm, I removed the redundant USEC ENDUSEC in assembly portion of the code. It makes it look cleaner and you only need the one set. SUB mix_asm(STRING fullkey) USEC #define ASM asm( #define INTEL ); ASM ".intel_syntax noprefix;" "mov ecx,4;" ".swaps:;" "mov al,byte[edi-1];" "xchg al,byte[edi+7];" "stosb;" "inc edi;" "loop .swaps;" ".att_syntax;" : :"D"(fullkey) :"eax", "ecx" INTEL
END USEC END SUB
BR greco558 Attachments:vaultasm.tar (30 KB)
|
|