ASMin28Days Help
Moderator: MaxCoderz Staff
ASMin28Days Help
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.
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.
(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.
- elfprince13
- Sir Posts-A-Lot
- Posts: 234
- Joined: Sun 11 Dec, 2005 2:21 am
- Contact:
-
- Calc King
- Posts: 1513
- Joined: Sat 05 Aug, 2006 7:22 am
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
2 bytes, 8 cc's
3 bytes, 11 cc's
3 bytes, 12 cc's
now 16bits:
6 bytes, 29 cc's
(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
Code: Select all
cpl
inc a
Code: Select all
xor $FF
inc a
Code: Select all
ld b,a
xor a
sub b
6 bytes, 29 cc's
Code: Select all
ld hl,0
or a
sbc hl,de
;note: negates DE but stores in HL
Last edited by King Harold on Fri 30 Mar, 2007 8:14 am, edited 3 times in total.
-
- Calc King
- Posts: 1513
- Joined: Sat 05 Aug, 2006 7:22 am
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)
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)
- driesguldolf
- Extreme Poster
- Posts: 395
- Joined: Thu 17 May, 2007 4:49 pm
- Location: $4080
- Contact:
if you're forced to not use some 16bit regs (hl for example) you could negate bc like this:
this is quit long but it only destroys a,
note that I only know this because of 83pa28d
Code: Select all
ld a, b
cpl
ld b, a
ld a. c
cpl
ld c, a
inc bc
note that I only know this because of 83pa28d