It is currently Thu Oct 19, 2017 9:44 am

 All times are UTC - 7 hours

 Page 1 of 1 [ 9 posts ]
 Print view Previous topic | Next topic
Author Message
 Post subject: Multiplication (signed/unsigned)Posted: Thu Feb 03, 2005 4:26 am

Joined: Thu Feb 03, 2005 4:23 am
Posts: 8
I've got a working multiplication routine (I think?), does it need editing to work with both signed and unsigned values? I've only tested it with unsigned values:

arg0 and arg1 are the numbers, res0 is the result. (the _0 is the low (only) byte in 8-bit, the same memory locations are being used for 16-bit calculations as well)

Code:
mul88:
clc
lda #00
sta res0_0
sta arg1_0
ldx #8
mul88_2:
lsr arg0_1
bcc mul88_3
clc
lda res0_0
sta res0_0
mul88_3:
asl arg0_1
ror arg1_0
dex
bne mul88_2
rts

Top

 Post subject: Posted: Thu Feb 03, 2005 11:48 am

Joined: Mon Sep 20, 2004 6:04 am
Posts: 3470
Location: Indianapolis
Yeah, signed multiplication would be different. Maybe a little more complex. I haven't used signed stuff very much, so hopefully someone else has better info than me.

I've used a multiplication routine that's pretty much like yours, except I removed the DEX / BNE part and made it an unrolled loop for a little extra speed. (42 cycles or so?)

Code:
multiply:       ; 8-bit multiply.

lda #0
sta result_lo

lsr mult1
bcc :+
clc
:
ror
ror result_lo

lsr mult1
bcc :+
clc
:
ror
ror result_lo

lsr mult1
bcc :+
clc
:
ror
ror result_lo

lsr mult1
bcc :+
clc
:
ror
ror result_lo

lsr mult1
bcc :+
clc
:
ror
ror result_lo

lsr mult1
bcc :+
clc
:
ror
ror result_lo

lsr mult1
bcc :+
clc
:
ror
ror result_lo

lsr mult1
bcc :+
clc
:
ror
ror result_lo

sta result_hi

rts

Top

 Post subject: Posted: Thu Feb 03, 2005 8:58 pm

Joined: Thu Feb 03, 2005 4:23 am
Posts: 8
Does NESASM support those types of labels? (non-literal labels)

Top

 Post subject: Posted: Thu Feb 03, 2005 9:31 pm

Joined: Wed Nov 10, 2004 6:47 pm
Posts: 1845
eeewwww.... nesasm

awful assembler

Top

 Post subject: Posted: Thu Feb 03, 2005 11:33 pm

Joined: Thu Feb 03, 2005 4:23 am
Posts: 8
Which assembler do you use?

Top

 Post subject: Posted: Fri Feb 04, 2005 12:43 am

Joined: Mon Sep 20, 2004 6:04 am
Posts: 3470
Location: Indianapolis
I don't think there are unnamed labels in NESASM.

I use CA65.

Really, the code I posted would look a lot nicer if the main part was a macro and the label was local to the macro.

Top

 Post subject: Posted: Fri Feb 04, 2005 2:06 pm

Joined: Thu Feb 03, 2005 4:23 am
Posts: 8
Does anyone else know about signed multiplication routines?

Top

 Post subject: Posted: Fri Feb 04, 2005 2:17 pm

Joined: Wed Nov 10, 2004 6:47 pm
Posts: 1845
Couldn't you just take the absolute value of both factors and then figure out whether the product should be positive or negative and adjust it accordingly?

like check each factor -- if one negative, set a flag and convert the number to its positive counterpart (EOR #\$FF CLC ADC #\$01) -- do normal, unsigned multiplication with the two factors... and if the product is supposed to be negative (one factor -- but not both negative), negate it.

edit: code to help suggest the idea in case my explaination isn't clear. This isn't the quickest way to do it, but it should give the general idea

Code:
CLC
LDA #\$00
STA sign_flag

LDA factor_1
BPL :+
EOR #\$FF
STA factor_1
LDA sign_flag
EOR #\$01
STA sign_flag

:
LDA factor_2
BPL :+
EOR #\$FF
STA factor_2
LDA sign_flag
EOR #\$01
STA sign_flag

:
JSR unsigned_mult_routine

LDA sign_flag
BEQ :+

CLC
LDA product_lo
EOR #\$FF
STA product_lo
LDA product_hi
EOR #\$FF
STA product_hi

:
RTS

Top

 Post subject: Posted: Fri Feb 04, 2005 10:08 pm

Joined: Thu Feb 03, 2005 4:23 am
Posts: 8
That makes sense, I'll check it and post my results when I get back. It's Friday night guys, party!

Thanks for your help, I assume the same method can be done with division?

Thanks,
Chris

By the way, is there a better way for doing 8-bit multiplication (without using tables, or should I?) That method I am using is basically just 16-bit one modified.
[/edit]

Top

 Display posts from previous: All posts1 day7 days2 weeks1 month3 months6 months1 year Sort by AuthorPost timeSubject AscendingDescending
 Page 1 of 1 [ 9 posts ]

 All times are UTC - 7 hours

#### Who is online

Users browsing this forum: Google [Bot] and 9 guests

 You cannot post new topics in this forumYou cannot reply to topics in this forumYou cannot edit your posts in this forumYou cannot delete your posts in this forumYou cannot post attachments in this forum

Search for:
 Jump to:  Select a forum ------------------ NES / Famicom    NESdev    NESemdev    NES Graphics    NES Music    Homebrew Projects       2017 NESdev Competition       2016 NESdev Competition       2014 NESdev Competition       2011 NESdev Competition    Newbie Help Center    NES Hardware and Flash Equipment       Reproduction    NESdev International       FCdev       NESdev China       NESdev Middle East Other    General Stuff    Membler Industries    Other Retro Dev       SNESdev       GBDev    Test Forum Site Issues    phpBB Issues    Web Issues    nesdevWiki