flat assembler
Message board for the users of flat assembler.

Index > Windows > HELP (NEW TO ASSEMBLY)

Author
Thread Post new topic Reply to topic
edprog



Joined: 07 Feb 2011
Posts: 19
edprog
Hello All,

Last night Revolution was of great help showing me how to use FASM and also by helping me understand the program where I can see what happens as it runs. But now i am trying to make this program compile.. The function is supposed to compute the sum of absolute values of an array. It gives me the .EXE (has stopped unexpectedly when ran)

Thank you Guys

Code:
include 'win32ax.inc'

.data
values: 
        dd      -3,-4,6,-8,10
        ;The top line declares a variable to store some values
        count   =5
        ; a count of 5
.code 

start: 
        ccall  AbsSum,values,count
        ;return value is in eax 
        invoke  ExitProcess,eax 

AbsSum:
        push    ebp     ;Pushing into stack
        mov     ebp,esp ;Initializing stack pointer
        mov     ecx,[ebp+8] ; ecx = Start
        mov     edx,[ebp+12]; edx = Count
        xor     eax,eax ;  sum = 0
        test    edx,edx
        je      .L34
    .L35: 
        add     eax,[ecx] ; add *Start to Sum
        add     edi, 0    ;0
        sub     edi,[eax]
        cmovg    eax,[edi]
        add     ecx,4 ; Start ++
        dec     edx ; Count --
        jnz     .L35   ;Stop when 0
    .L34: 
        mov     esp,ebp 
        pop     ebp  ; Pop stack
        ret         ; Return value

.end start
    
Post 08 Feb 2011, 05:43
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17658
Location: In your JS exploiting you and your system
revolution
You have to save and restore edi if you want to comply with ccall convention. See the code for absolute value below.
Code:
include 'win32ax.inc'

.data
values:
      dd      -3,-4,6,-8,10
       ;The top line declares a variable to store some values
      count   =5
  ; a count of 5
.code

start:
   ccall  AbsSum,values,count
  ;return value is in eax
     invoke  ExitProcess,eax

AbsSum:
  push    ebp     ;Pushing into stack
 mov     ebp,esp ;Initializing stack pointer
 push    edi     ;ccall convention is to preserve edi, esi and ebx
   mov     ecx,[ebp+8] ; ecx = Start
   mov     edx,[ebp+12]; edx = Count
   xor     eax,eax ; sum = 0
   test    edx,edx
     je      .L34
    .L35:
       mov     edi,[ecx]       ;edi=value
  neg     edi             ;edi=-value
 cmovs   edi,[ecx]       ;edi=abs(value)
     add     eax,edi
     add     ecx,4   ; Start ++
  dec     edx     ; Count --
  jnz     .L35    ;Stop when 0
    .L34:
       pop     edi     ;restore edi
        mov     esp,ebp
     pop     ebp     ; Pop stack
 ret             ; Return value

.end start    
Post 08 Feb 2011, 06:46
View user's profile Send private message Visit poster's website Reply with quote
edprog



Joined: 07 Feb 2011
Posts: 19
edprog
Wow i was totally doing it wrong,,, And I was also using sub, cmovg etc.. lol... thank you very much for your help!!! this is really helpful
Post 08 Feb 2011, 06:56
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17658
Location: In your JS exploiting you and your system
revolution
Note that cmov is not available on all 32-bit CPUs. This may or may not affect you, but is something to be aware of.
Post 08 Feb 2011, 07:21
View user's profile Send private message Visit poster's website Reply with quote
edprog



Joined: 07 Feb 2011
Posts: 19
edprog
Thank you for helping me understand FASM, I wouldnt have done it without you!

Regards
Post 08 Feb 2011, 07:26
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2020, Tomasz Grysztar. Also on GitHub, YouTube, Twitter.

Website powered by rwasa.