flat assembler
Message board for the users of flat assembler.

Index > Windows > finding the numbers of negative and positive numbers

Author
Thread Post new topic Reply to topic
enterprevic



Joined: 05 Jun 2020
Posts: 3
enterprevic 05 Jun 2020, 08:14
The code below is meant to print out the numbers of negative and positive numbers in the array, to me the code seems alright but unfortunately it doesn't work which seems there's a mistake somewhere.

And I don't really understand where I made the mistake, the code is meant to work on FASM

Code:
format PE console ; 
entry start ; 

include 'include\win32a.inc'

section '.idata' import data readable ; 

library kernel,'kernel32.dll',\
msvcrt,'msvcrt.dll'

import kernel,\
ExitProcess,'ExitProcess'

import msvcrt,\
printf,'printf',\
getchar,'_fgetchar'

section '.data' data readable writeable ; 

array dw 2, 8, 2, -6, 14, 5, 6, 17, 4, 14, -4

len equ $-array

MESSAGE db "Negative %d",0
MESSAGE2 db "Positive %d", 0

section '.code' code readable executable ; 

proc_pos_and_neg:
  mov   esi, array ; esi points to the array.  In MASM, use OFFSET word_array
  xor   edx, edx           ; pos_count = 0
  xor   eax, eax           ; neg_count = 0
  mov   ebp, -1            ; constant to test with

  lea   edi, [esi + len]  ; points one past the end of the array
  xor   ebx, ebx          ; clear upper portion, because setcc r32 isn't available, only setcc r8  Sad

countloop:
  test   bp, word [esi]   ; test reg, mem can micro-fuse, but test mem, imm can't
  setg   bl               ; 0 or 1, depending on  array[i] > 0
  lea    edx, [edx + ebx]  ; add without affecting flags
  setl   bl
  add    eax, ebx          ; can clobber flags now

  add    esi, 2            ; extra loop overhead compared to the scan-backwards decrementing an index reg I used last time
  cmp    esi, edi
  jb  countloop            ; loop while our pointer is below the pointer to one-past-the-end

      ; neg_count in eax,  pos_count in edx

ccall [printf], MESSAGE, eax
ccall [printf], MESSAGE, edx
ccall [getchar]
stdcall [ExitProcess],0    
Post 05 Jun 2020, 08:14
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20340
Location: In your JS exploiting you and your system
revolution 05 Jun 2020, 08:25
You didn't say exactly what problem you have so I'll wing it from what I see above ...

You print the same MESSAGE twice. I presume the second printf is meant to be MESSAGE2.

Also, ccall will corrupt the values in registers eax, ecx, and edx. So the value in edx is now lost after the first ccall. You can save it another register like esi or edi
Code:
mov edi,edx
ccall [printf], MESSAGE, eax
ccall [printf], MESSAGE2, edi    
I can't find where you define the start label. Perhaps try:
Code:
entry proc_pos_and_neg    
Post 05 Jun 2020, 08:25
View user's profile Send private message Visit poster's website Reply with quote
enterprevic



Joined: 05 Jun 2020
Posts: 3
enterprevic 05 Jun 2020, 10:28
revolution wrote:
You didn't say exactly what problem you have so I'll wing it from what I see above ...

You print the same MESSAGE twice. I presume the second printf is meant to be MESSAGE2.

Also, ccall will corrupt the values in registers eax, ecx, and edx. So the value in edx is now lost after the first ccall. You can save it another register like esi or edi
Code:
mov edi,edx
ccall [printf], MESSAGE, eax
ccall [printf], MESSAGE2, edi    
I can't find where you define the start label. Perhaps try:
Code:
entry proc_pos_and_neg    


The program doesn't compile from CMD and I'm thinking it's a problem from the code, so I don't really know if it's a problem from the code or my PC
Post 05 Jun 2020, 10:28
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20340
Location: In your JS exploiting you and your system
revolution 05 Jun 2020, 10:33
What error message do you get when you compile?
Post 05 Jun 2020, 10:33
View user's profile Send private message Visit poster's website Reply with quote
enterprevic



Joined: 05 Jun 2020
Posts: 3
enterprevic 05 Jun 2020, 17:42
revolution wrote:
What error message do you get when you compile?

I get an error saying it can't run on your PC, and I even tried recompiling assembly programs I compiled earlier and was getting the same error message
Post 05 Jun 2020, 17:42
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20340
Location: In your JS exploiting you and your system
revolution 05 Jun 2020, 22:30
What do you see if you just type fasm at the command prompt? Do you see fasm print the help text?
Post 05 Jun 2020, 22:30
View user's profile Send private message Visit poster's website 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-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.