Author Topic: Crambly - NASM Wrapper Language  (Read 9470 times)

Offline Mixolydian

  • Jr. Member
  • *
  • Posts: 21
Re: Crambly - Crappy Assembly
« Reply #15 on: June 28, 2014, 03:07:22 PM »
Sorry for the wait, I had some paid programming work so obviously that took priority.

I finally found the time to rewrite Crambly from the ground up and I am pleased to announce its release!

See the first post for the download link and a sample Crambly program which shows off all of the features I've been working on.
« Last Edit: June 28, 2014, 05:51:38 PM by Mixolydian »

Offline gammac

  • Jr. Member
  • *
  • Posts: 71
  • Country: 00
Re: Crambly - Crappy Assembly
« Reply #16 on: June 29, 2014, 11:12:29 AM »
Hi,

nice work, but ...
Code: [Select]
_main:
push ebp
mov ebp, esp
and esp, -16
mov dword [ebp], 5
mov dword [ebp-4], 2

what are you doing here?

local variables should look like this:

Code: [Select]
sub esp, 8    ; reserve enough bytes for my local vars
mov dword [ebp-4], 5
mov dword [ebp-8], 2

And if you intend to use resd as directive for local vars, I would say, that's not a good idea.

btw: I would prefere an output that preseves the original source formating. Only your generated source should start at the beginning of a line, everything else as founded in the original source file.

EDIT: and you have left the function arguments on the stack
« Last Edit: June 29, 2014, 11:27:44 AM by gammac »
Please comment your code! It helps to help you.

Offline Mixolydian

  • Jr. Member
  • *
  • Posts: 21
Re: Crambly - Crappy Assembly
« Reply #17 on: June 29, 2014, 12:45:48 PM »
Thanks for the feedback, I will fix the local variables.

Quote
you have left the function arguments on the stack

Originally I was going to leave this for the user to clean up, but I haven't fully decided.

Quote
I would prefere an output that preseves the original source formating.

Yes, I will definitely implement this in the future.

EDIT: I've just made some changes based on your suggestions. Here's the new adding.asm file that is generated:

Code: [Select]
extern _printf
global addFunction
global _main
section .data
addingString: db "Adding %d and %d", 10, "", 0
resultString: db "Result is %d", 10, "", 0
section .bss
result: resd 1
section .text
addFunction:
push ebp
mov ebp, esp
and esp, -16
push dword [ebp+12]
push dword [ebp+8]
push addingString
call _printf
add esp, 3*4
mov eax, [ebp+8]
add eax, [ebp+12]
add esp, 8
mov esp, ebp
pop ebp
ret
_main:
push ebp
mov ebp, esp
and esp, -16
sub esp, 16
mov dword [ebp-4], 5
mov dword [ebp-8], 2
push dword [ebp-8]
push dword [ebp-4]
call addFunction
mov dword [result], eax
push dword [result]
push resultString
call _printf
add esp, 2*4
add esp, 16
mov esp, ebp
pop ebp
ret

Quote
And if you intend to use resd as directive for local vars, I would say, that's not a good idea.

I could change the the res to loc, so it would be:

Code: [Select]
locd test
mov dword [test], 5
« Last Edit: June 29, 2014, 12:58:48 PM by Mixolydian »

Offline Mixolydian

  • Jr. Member
  • *
  • Posts: 21
Re: Crambly - NASM Wrapper Language
« Reply #18 on: June 29, 2014, 01:24:42 PM »
I've just uploaded Version 0.2 to GitHub.

Local variables are now accessed correctly, function parameters are now taken off the stack before returning, and local variables are declared with locb, locw, locd, and locq instead of resb, resw, resd, and resq.

EDIT: I forgot to mention that the reason I sub esp, 16 instead of just sub esp, 8 is because Crambly automatically aligns to multiples of 16 to support 64bit, this is the same thing that gcc does; for example:

locb variable

Reserves 16 bytes, even though we only use 1.

But:

locq variable1
locq variable2
locb variable3

Reserves 32 bytes, even though we only use 17.
« Last Edit: June 29, 2014, 01:34:41 PM by Mixolydian »

Offline gammac

  • Jr. Member
  • *
  • Posts: 71
  • Country: 00
Re: Crambly - NASM Wrapper Language
« Reply #19 on: June 30, 2014, 07:05:53 AM »
Code: [Select]
extern _printf
global addFunction
global _main

section .data
addingString: db "Adding %d and %d", 10, "", 0
resultString: db "Result is %d", 10, "", 0

section .bss
result: resd 1

section .text
addFunction:
push ebp
mov ebp, esp
and esp, -16

push dword [ebp+12]
push dword [ebp+8]
push addingString
call _printf
add esp, 3*4
mov eax, [ebp+8]
add eax, [ebp+12]

add esp, 8            ; that isn't a cleanup
mov esp, ebp
pop ebp
ret   ; no callee cleanup
_main:
push ebp
mov ebp, esp
and esp, -16
sub esp, 16

mov dword [ebp-4], 5
mov dword [ebp-8], 2
push dword [ebp-8]
push dword [ebp-4]
call addFunction
; and no caller clean up
mov dword [result], eax
push dword [result]
push resultString
call _printf
add esp, 2*4

add esp, 16          ; that's not necessary
mov esp, ebp        ; stack ballancing is done here
pop ebp
ret

You still have'nt fixed the clean up, take a look at this:
http://en.wikipedia.org/wiki/X86_calling_conventions#Callee_clean-up

and take a look at your stack balancing
Please comment your code! It helps to help you.

Offline Mixolydian

  • Jr. Member
  • *
  • Posts: 21
Re: Crambly - NASM Wrapper Language
« Reply #20 on: June 30, 2014, 04:58:35 PM »
Thanks for helping me so much; I think I've finally got everything working correctly now, please try the latest Version 0.4 and tell me if there are any bugs still present.

Offline gammac

  • Jr. Member
  • *
  • Posts: 71
  • Country: 00
Re: Crambly - NASM Wrapper Language
« Reply #21 on: July 01, 2014, 09:19:38 AM »
sorry, I haven't tried it. But I've two notes, not really bugs.

- because you tried to align the stack you should know that your local vars are unaligned. Was it intended?
- ret 0 is not necessary maybe it's slower then simply ret

I assume that your crappy tool is not written in assembly. Which language do you use for it?

Code: [Select]
_main:
push ebp
mov ebp, esp
and esp, -16
sub esp, 16

mov dword [ebp-4], 5         ; ebp is unaligned esp
mov dword [ebp-8], 2
push dword [ebp-8]
push dword [ebp-4]
call addFunction
;...
mov esp, ebp
pop ebp
ret 0          ; it's not necessary with 0, it could be slower
ret            ; then this.


Please comment your code! It helps to help you.

Offline Mixolydian

  • Jr. Member
  • *
  • Posts: 21
Re: Crambly - NASM Wrapper Language
« Reply #22 on: July 02, 2014, 06:57:26 PM »
The tool is written in C - I will release the source, but not right now. It's not ready yet.

Regarding the incorrect alignment:

Code: [Select]
sub esp, 16

mov dword [ebp-4], 5         ; ebp is unaligned esp
mov dword [ebp-8], 2

Should it be this?

Code: [Select]
sub esp, 16

mov dword [ebp-12], 5
mov dword [ebp-8], 2

Code: [Select]
ret 0          ; it's not necessary with 0, it could be slower
ret            ; then this.

OK; that's a simple fix.

I'm really busy for the rest of the week and I'm away on the weekend but I'll try and get an update fixing these two "notes" when I can.

Once again, thanks for the feedback.
« Last Edit: July 02, 2014, 06:59:14 PM by Mixolydian »

Offline gammac

  • Jr. Member
  • *
  • Posts: 71
  • Country: 00
Re: Crambly - NASM Wrapper Language
« Reply #23 on: July 03, 2014, 06:49:29 AM »

Should it be this?

Code: [Select]
sub esp, 16

mov dword [ebp-12], 5
mov dword [ebp-8], 2

No.

Quote

I'm really busy for the rest of the week and I'm away on the weekend but I'll try and get an update fixing these two "notes" when I can.


I understand you but If you want to understand the stack and what goes on with the stack frame you'll need a little bit time to go for it.

Please comment your code! It helps to help you.

Offline Mixolydian

  • Jr. Member
  • *
  • Posts: 21
Re: Crambly - NASM Wrapper Language
« Reply #24 on: July 24, 2014, 10:55:53 AM »
What should it be?

Offline gammac

  • Jr. Member
  • *
  • Posts: 71
  • Country: 00
Re: Crambly - NASM Wrapper Language
« Reply #25 on: July 26, 2014, 12:03:18 PM »
Let the user do the work, if they need aligned local variables.




Please comment your code! It helps to help you.