
Educators: Earn a free Gold upgrade by joining the PBwiki Back To School Challenge.

Questions? Join PBwiki's weekly office hours today at 1 PM Eastern and get live answers.
In the Roots 1 program, it is possible for the answer to be negative. a = 1, b = 4, , c = 1. but you don't get -15!!!
What do you get?
Every time , i tried to compute numbers in which b^2 < 4ac, I get a postive back.
Why?
What can you do to fix it?
I haven't solve the problem but let see if somebody can help me with it. I was testing some numbers to see why i couldn't get negative so I try this: Let a = 4, b = 8, and c = 10, we get:
x = b^2 - 4ac = (8^2) - (4*4*10) = 64 - 160 = -96
but my code gives me:
x = b^2 - 4ac = (8^2) - (4*4*10) = 160 //Weird right? Now if you look carefully 256 - 96 = 160. Also, 2^8 = 256 and the al register is 8 bit too.
So this lead me to look the hex representation of 160 an -96, and guess what? both have the same value:
160 = A0h = 10100000b2.
-96 = A0h = 10100000b2.
This is correct. see the comment - rg
So my ouput was right! the problem is how PutDec figure out if it is 160 or - 96 . Also on signed 8 bit integer , you cannot represent 160 , but -96 it is possible.
If you read chapter 1, there 3 ways of reprensenting negative numbers, they are: by Magnitude, by 1 complement, and by 2 complement. but how can we do this?
Because if you get a positive value which is correct, then you would get its negation by complementing it right?
I hope we can solve this cool problem!
Armando Fonseca.
Updated(March 04, 2008)
PROBLEM: Every time , i tried to compute numbers in which b^2 < 4ac, I get a postive back.
SOLUTION:
Remark1-->Depending on how you wrote the code there might be a small difference why my program and yours were not giving negative numbers when it was supposed to. I would give a general answer , so you and our classmates can relate to it as well.
Regardless what is on ax, when you do an 8 bit multiplication let say:
imul mem/reg ; Assuming there is an integer on the al register.
The product becomes a 16 bit number ! Let me give you an example:
Let's multiply -32 * 3 = -96 <==> E0h*3h = A0h ,According to page 66 of the textbook the product becomes a 16b number(al * mem/reg = ax) , and if you se above A0h = 160 too.
I did some testing , and i found that our imul by default storage in the ax = 00A0h instead of FFA0h. So how can we solve it?
Two ways:
Solution1: one mov to the ah 255 --> mov ah, 255
Solution2: use the sign extension instruction . For this 8 bit number use cbw.
Remark2--> I'm not sure if they are the right ones because i'm a student too, but this two solutions solve my problem.
Remark 3--> Actually , i did think about the solution 1 before today.'s class. I askedProfessor Glass on class if this has to do with the sign extension, but he told me is simpler than than.... may be , I he tought i was asking something else.
Armando Fonseca
Solution 2 is the correct / best way. that is why the CBW instruction is there. Solution 1 is basically what the CBW instruction does for negative numbers. Realize that Solution 1 only works for negative where CBW would work for both positive and negative numbers filling with 0's or 1's accordingly.
As for remark 3, :-) I said that there was a simpler solution to what you stated the fix should be not that your explaination that it was sign extension problem.
good. problem is closed :-)
PS: Some of you may have noticed that one of these wiki questions was on the exam :-)
Page Information
|
Wiki Information |
Recent PBwiki Blog Posts |