Page 1 of 1

ASMin28Days Help

Posted: Wed 28 Mar, 2007 9:28 pm
by Mexi1010
I just have one little question, (I hope I do not sound like a random noob) but could someone help me understand the Day 2 tutorial on the 28 day tutorial because I get a brain fart when I get to the negatives part.

Posted: Wed 28 Mar, 2007 11:24 pm
by Liazon
Do you mean Day 3?

(kinda watered down and not very precise but...)

Well, if you understand that a number and it's additive inverse must add up to the additive identity, you're all set. Basically, x + -x must = 0 right? So in computer logic, that must also be true. However, the way computer logic is designed to store numbers, it can only add and truncate (cut digits off) at the primitive level.

So people were like have 50 + 206 (aka -5) = 256. And since 256 = %1 00000000, the one gets cut off because only the right eight digits can fit into a byte (%00000000). Not only is this convenient, people have found that it works (on the basis of modular arithmetic iirc) for all other subtractions.

Now the reason why all negatives have a 1 in the left most bit is because what would happen if you tried to take the negative of 255 (%11111111)? You'd get %00000001. How would you know when have 1 or -255? Well you don't, so that's why you have to 1.) decide whether or not you will do signed math 2.) any number greater than %01111111... will be deemed negative.

The big thing is that ASMin28 has an annoying typo that says 5+206 = 256, and you probably can tell it's supposed to be 50+206.

I don't know if I helped, so just reply back.

Posted: Thu 29 Mar, 2007 12:18 am
by elfprince13
well spake liazon.

Posted: Thu 29 Mar, 2007 12:22 am
by Jim e
Liazon wrote: So people were like have 50 + 206 (aka -5) = 256.
typo: (aka -50)


You should ask more specific questions, you'll get more specific answers. What in particular is confusing you about negatives?

Posted: Thu 29 Mar, 2007 1:13 am
by Mexi1010
I will try to read through all that but I think that helps (and yeah it was day 3)

Posted: Thu 29 Mar, 2007 1:21 pm
by King Harold
you could think of a negative number as a number which once added to a a number bigger than its positive is big enough to cause sufficient overflow so that the result would appear as if the positive of that added negative was subtracted.
(damn I didn't mean to write something as unreadable as that..)

example:
7 + (-5) = 2
so the binary representation of -5 must be big enough to overflow 7 enough to result in 2, which is $FF - 5 +1 = $FB (note that the sign is set), $FB + 7 would overflow and become 2. The negative can also be calculated by inverting all bits and adding 1, there are several ways to do this in asm (compilers usually support negatives as well):
note: except for the actual opcode, the first and second ways are exactly the same (in both size and execution time)
2 bytes, 8 cc's

Code: Select all

neg
2 bytes, 8 cc's

Code: Select all

cpl
inc a
3 bytes, 11 cc's

Code: Select all

xor $FF
inc a
3 bytes, 12 cc's

Code: Select all

ld b,a
xor a
sub b
now 16bits:
6 bytes, 29 cc's

Code: Select all

ld hl,0
or a
sbc hl,de
;note: negates DE but stores in HL

Posted: Thu 29 Mar, 2007 8:40 pm
by Liazon
Well said King Harold, and lots of other useful info too :)

Posted: Fri 30 Mar, 2007 6:59 am
by Spencer
careful ...

ld hl,0
or a
sbc hl,de

Posted: Fri 30 Mar, 2007 8:12 am
by King Harold
lol yes I was thinking too much about the "invert and inc"
fixed now..

and never forget the "or a" unless you're absolutely sure the carry flag is not set (after additions which could not have carried, boolean operations, after jumping somewhere with the condition NC or jumping after a compare with the condition Z or NC)

Posted: Thu 17 May, 2007 5:25 pm
by driesguldolf
if you're forced to not use some 16bit regs (hl for example) you could negate bc like this:

Code: Select all

ld a, b
cpl
ld b, a
ld a. c
cpl
ld c, a
inc bc
this is quit long but it only destroys a,
note that I only know this because of 83pa28d