ASMin28Days Help

Got questions? Got answers? Go here for both.

Moderator: MaxCoderz Staff

Post Reply
Mexi1010
New Member
Posts: 5
Joined: Tue 06 Mar, 2007 2:48 am
Location: Programmer Fanatic

ASMin28Days Help

Post 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.
Liazon
Calc Guru
Posts: 962
Joined: Thu 27 Oct, 2005 8:28 pm

Post 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.
Image Image Image
User avatar
elfprince13
Sir Posts-A-Lot
Posts: 234
Joined: Sun 11 Dec, 2005 2:21 am
Contact:

Post by elfprince13 »

well spake liazon.
User avatar
Jim e
Calc King
Posts: 2457
Joined: Sun 26 Dec, 2004 5:27 am
Location: SXIOPO = Infinite lives for both players
Contact:

Post 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?
Image
Mexi1010
New Member
Posts: 5
Joined: Tue 06 Mar, 2007 2:48 am
Location: Programmer Fanatic

Post by Mexi1010 »

I will try to read through all that but I think that helps (and yeah it was day 3)
King Harold
Calc King
Posts: 1513
Joined: Sat 05 Aug, 2006 7:22 am

Post 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
Last edited by King Harold on Fri 30 Mar, 2007 8:14 am, edited 3 times in total.
Liazon
Calc Guru
Posts: 962
Joined: Thu 27 Oct, 2005 8:28 pm

Post by Liazon »

Well said King Harold, and lots of other useful info too :)
Image Image Image
Spencer
Extreme Poster
Posts: 346
Joined: Mon 17 Jan, 2005 8:56 am
Location: Indiana

Post by Spencer »

careful ...

ld hl,0
or a
sbc hl,de
King Harold
Calc King
Posts: 1513
Joined: Sat 05 Aug, 2006 7:22 am

Post 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)
User avatar
driesguldolf
Extreme Poster
Posts: 395
Joined: Thu 17 May, 2007 4:49 pm
Location: $4080
Contact:

Post 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
Post Reply