Trouble with code project

Page 1 of 1

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

#1 CleverName

Reputation: 0
• 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
```

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

And moved to the Assembly forum.

Is This A Good Question/Topic? 0

Replies To: Trouble with code project

#2 blackcompe

• D.I.C Lover

Reputation: 1156
• 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

#3 CleverName

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

Re: Trouble with code project

Posted 22 April 2012 - 04:32 PM

blackcompe, 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

• Saucy!

Reputation: 6171
• Posts: 23,845
• Joined: 23-August 08

Re: Trouble with code project

Posted 22 April 2012 - 04:33 PM

No, I'll move it.

#5 GunnerInc

• "Hurry up and wait"

Reputation: 899
• Posts: 2,337
• 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