Omnimaga
Calculator Community => TI Calculators => ASM => Topic started by: ben_g on October 02, 2011, 03:24:44 pm
-
Hi,
I'm trying to do a 16 by 24bit signed addition for my z80 program. Now I have this code: ld hl,(var1)
ld de, (var2)
ld a, h
rla
sbc a,a
add hl, de
ld b, a
ld a, (var1+2)
adc a, b
ld (var1+2), a
It seems to work in some situations, but it also sometimes gives other results.
What am I doing wrong?
btw: var1 is the 24bit var, var2 the 16bit one. I haven't fully written this routine, a part of it comes from a load 16bit to 24bit routine from calc84maniac.
many thanks in avantage.
-
You need to sign extend using the top bit of DE, not of HL. Replacing ld a,h with ld a,d should fix your problem.
-
Thanks, but it still doesn't fully work. (It gets the same results, actually.
-
Wait, so are you adding 24 bits at (var1) and 16 bits at (var2)?
If that is the case, does this work (I have not tested this!):
ld hl,(var1)
ld de, (var2)
add hl,de
sbc a,a
ld b,a
ld a,(var1+2)
sub b
ld (var1+2),a
The result is in AHL and does essentially the same thing (I believe) with two bytes less (I think).
EDIT: Is there a particular reason you are only saving the upper 8 bits and not the lower 16?
-
Thanks, but it still doesn't fully work. (It gets the same results, actually.
Oh, it looks like you forgot to save the HL result back to var1. Also Xeda, I don't think that code works for a signed 16-bit value (that looks like it could work for unsigned though)
-
Oh, right, signed XD. Sorry, I missed that. I don't think I've ever made a signed math routine O.O
-
That also gives the same result...
BTW: I do save the lower 16 bits. I just forgot to copy the last line, which is :ld (tx2), hl
EDIT: double ninja'd O.O I should try to type faster...
-
Haha, you edit ninja'd me about saving the result. Also, here's a slightly more optimized unsigned addition:
ld hl,(var1)
ld de, (var2)
add hl,de
ld (var1),hl
ld a,(var1+2)
adc a,0
ld (var1+2),a
Edit: That post was aimed at Xeda, btw.
ben_g, I'm not sure what the problem could be then.
-
But if xeda's code only works for unsigned numbers, and it gave the same result as mine, does that mean if I'm just handeling the sign incorrectely? If so, then what am I doing wrong with the sign?
EDIT: I'm also not entirely sure if it's the addition that causes the problem, but as the rest of the code is almost simular to my previous triangle drawing routine (16bit and 8bit), eccept for the addition and the storing to variables.
This is what it does:
(http://picturestack.com/586/20/Zqsprobleemnpv.gif)
I made the two triangles 50% transparent so you can see both of them well. They should form a rectangle, but my triangle drawing routine fails on one of them.
-
I am pretty sure this code would work for signed:
ld hl,(var1)
ld de, (var2)
ld a, d
rla
sbc a, a
ld b, a
add hl, de
ld (var1), hl
ld a, (var1+2)
adc a, b
ld (var1+2), a
-
It still gives the exact same result :(
-
Could you give an example of an addition that doesn't work?
-
It's difficoult to calculate what walues are passed to the routine by the screenshot, so as logn as i can't get wabbitemu to debug, I can't tell which arguments are passed to it.
-
This are some of the results it gets, compared to those the windows calculator gets. The results from the calculator are formated as actual HEX numbers (Big endian), while the results from the routine are like they are stored to the memory(little endian). I've found 3 cases on which the result from the routine doesn't match those from the calculator.
Results from calculator
-----------------------------
00 19 00 00 00 00 19 00
00 19 00 01 8C 00 1A 8C
00 19 FF 01 80 00 1B 7F
00 1A 8C 01 8C 00 1C 18
00 49 00 FE 80 01 47 80
00 49 00 00 00 00 49 00
00 47 80 FE 80 01 46 00
00 49 00 2F 00 00 78 00
Results from routine
-----------------------------
In 1 In2 Out
00 19 00 00 00 00 19 00
00 19 00 8C 01 8C 1A 00
FF 19 00 80 01 7F 1B 00
8C 1A 00 8C 01 18 1C 00
00 49 00 80 FE 40 47 00 <- Wrong?
00 49 00 00 00 00 49 00
80 47 00 80 FE 00 46 00 <- Wrong?
00 49 00 00 2F 00 49 00 <- Wrong?
Is there a problem with the routine or did I calculate the numbers wrong?