4 Replies - 1478 Views - Last Post: 22 April 2012 - 05:14 PM

#1 CleverName  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 22-April 12

Trouble with code project

Posted 22 April 2012 - 04:22 PM

In this class i'm suppose to Create a procedure that receives a value N between 0 to 100. When the procedure is called, there should be a probability of N/100 that it clears the Zero flag. Write a program that asks the user to enter a probability value 0 to 100. The program should call your procedure 30 times, passing it the same probability value and displaying the value of the Zero flag after the procedure returns. I was able to complete my other programs fine but struggling with this one..

What is the error here?


segment .data
percent dd 100
num_interations dd 30
num_eflags_zeros dd 0
num_eflags_ones dd 0
iteration_num db 0

segment .text
global manipulate_zf

manipulate_zf:

cmp byte [iteration_num], 0
jnz skip_basic
finit ; initialize fpu
fild dword [esp+4] ; load probability on the fpu stack st0
fidiv dword [percent] ; divide probability by 100 and store it in st0
fimul dword [num_interations] ; multiply (probability/100) * 30 and store it in st0.
fist dword [num_eflags_zeros] ; Round this number , convert it to an integer and save it in memory.
mov eax, [num_interations] ; eax = 30
sub eax, [num_eflags_zeros] ; eax = 30 - num_eflags_zeros ie; eax = num_times eflags.zf = 1
mov [num_eflags_ones], eax
mov byte [iteration_num], 1

skip_basic:
pushfd
zf_0:
cmp dword [num_eflags_zeros], 0
jz zf_1
and dword [esp], 0
dec dword [num_eflags_zeros]
mov eax, 0
jmp done
zf_1:
cmp dword [num_eflags_ones], 0
jz done
or dword [esp], 0x40
dec dword [num_eflags_ones]
mov eax, 1
done:
popfd
ret ; return to the calling C procedure


MOD EDIT: Added code tags. When posting code...USE CODE TAGS!!!

:code:

And moved to the Assembly forum.

Is This A Good Question/Topic? 0
  • +

Replies To: Trouble with code project

#2 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1156
  • View blog
  • Posts: 2,538
  • Joined: 05-May 05

Re: Trouble with code project

Posted 22 April 2012 - 04:23 PM

Shouldn't you have posted this in the assembly forum? And please use [code] tags when posting code.

This post has been edited by blackcompe: 22 April 2012 - 04:23 PM

Was This Post Helpful? 0
  • +
  • -

#3 CleverName  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 22-April 12

Re: Trouble with code project

Posted 22 April 2012 - 04:32 PM

View Postblackcompe, on 22 April 2012 - 04:23 PM, said:

Shouldn't you have posted this in the assembly forum? And please use [code] tags when posting code.


That was my first post ever i'll repost in the right place thanks
Was This Post Helpful? 0
  • +
  • -

#4 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6092
  • View blog
  • Posts: 23,614
  • Joined: 23-August 08

Re: Trouble with code project

Posted 22 April 2012 - 04:33 PM

No, I'll move it.
Was This Post Helpful? 0
  • +
  • -

#5 GunnerInc  Icon User is offline

  • "Hurry up and wait"
  • member icon




Reputation: 858
  • View blog
  • Posts: 2,303
  • Joined: 28-March 11

Re: Trouble with code project

Posted 22 April 2012 - 05:14 PM

Quote

What is the error here?

You tell us! It doesn't print the correct result? The sky turns black when run? Program crashes? What do you expect to be the output and what is the output?

Right off the bat, you are using ESP directly, UNLESS you know what you are doing, set up a stack frame!!!

The argument to your proc is at esp + 4 the return address is at esp you are ANDing esp and ORing esp... what the? Also, when you push anything esp gets changed, like you are doing with pushfd

Simple Stack Frame:
MyProc:
    push    ebp
    mov     ebp, esp
    
    ; your args now start at [ebp + 8]
    ; you can also push and pop onto the stack and ebp will stay the same
    
    
    leave
    ret     4 * NUMBER_OF_ARGS

replace NUMBER_OF_ARGS with what else, the number of args to your proc
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1