KOMUNITAS HOBBIYST ELEKTRONIKA - KHE
Diskusi Elektronika => Kategori Mikrokontroller/Robotik => Mikrokontroller Seri ATMEL => Topik dimulai oleh: Iman Fushshilat pada Januari 09, 2011, 01:24:36 AM
-
permisi, mau minta tolong.
Barang kali ada yang punya subroutin perkalian 8 bit dengan 16 bit yang
mana hasil lebar data maksimalnya mampu 32 bit (8 bit x 16 bit = 32
bit)
saya udah coba nyari2 di internet dan menemukan beberapa cuma hasilnya
kurang memuaskan (tidak benar) .
contoh ini:
;====================================================================
; subroutine MUL816
; 8-Bit x 16-Bit to 32-Bit Product signed Multiply
; 2's Complement format
;
; input: r0 = multiplicand X
; r3, r2 = multiplier Y
;
; output: r3, r2, r1, r0 = product P = X x Y (r3 = sign extension)
;
; calls: Cr0, Cr2r3, Mr0r3
;
; alters: acc, C, Bits SignX & SignY
;====================================================================
MUL816: push b
anl PSW, #0E7H ; Register Bank 0
acall Cr0 ; 2's comp -> Mag/Sign
acall Cr2r3 ; 2's comp -> Mag/Sign
mov a, r0 ; load X low byte into acc
mov b, r2 ; load Y low byte into B
mul ab ; multiply
push acc ; stack result low byte
push b ; stack result high byte
mov a, r0 ; load X into acc again
mov b, r3 ; load Y high byte into B
mul ab ; multiply
pop 00H ; recall X*YL high byte
add a, r0 ; add X*YL high and X*YH low
mov r1, a ; save result
clr a ; clear accumulator
addc a, b ; a = b + carry flag
mov r2, a ; save result
pop 00H ; get low result
mov r3, #0
acall Mr0r3 ; Mag/Sign -> 2's Comp
pop b
ret
;=================================================================
; subroutine Cr0
; 8-Bit 2's Complement -> magnitude / Sign Bit Conversion
;
; input: r0 = signed byte
;
; output: r0 = magnitude
; Bit 21H = sign (21H is set if r0 is a negative number)
;
; alters: acc
;=================================================================
Cr0: mov a, r0 ; read X into accumulator
jb acc.7, Cr0a ; X is negative if bit 7 is 1
clr SignX ; clear sign bit if 'positive'
ret ; done
Cr0a: cpl a ; X negative, find abs value
inc a ; XA = complement(XT)+1
mov r0, a ; save magnitude
setb SignX ; set sign bit if 'negative'
ret
;====================================================================
; subroutine Cr2r3
; 16-Bit 2's Complement -> magnitude / Sign Bit Conversion
;
; input: r3, r2 = signed word
;
; output: r3, r2 = magnitude
; Bit 22H = sign (22H is set if negative number)
;
; alters: acc, C
;====================================================================
Cr2r3: mov a, r3 ; read high into accumulator
jb acc.7, c1a ; negative if bit 7 is 1
clr SignY ; clear sign bit if 'positive'
ret ; done
c1a: setb SignY ; set sign flag
mov a, r2 ; number is negative
cpl a ; complement
add a, #1 ; and add +1
mov r2, a
mov a, r3 ; get next byte
cpl a ; complement
addc a, #0
mov r3, a
ret
;====================================================================
; subroutine Mr0r3
; 32-Bit magnitude / Sign Bit -> 2's Complement Conversion
;
; input: r3, r2, r1, r0 = magnitude
; Bits 21H & 22H = sign bits of operands X and Y
; (set if negative)
;
; output: r3, r2, r1, r0 = signed word
;
; alters: acc, C
;====================================================================
Mr0r3: jb SignX, Mr0r3b ; test X sign
jb SignY, Mr0r3a ; test Y sign
ret
Mr0r3b: jnb SignY, Mr0r3a
ret
Mr0r3a: mov a, r0 ; negate number
cpl a ; complement
add a, #1 ; and add +1
mov r0, a
mov a, r1 ; get next byte
cpl a ; complement
addc a, #0
mov r1, a
mov a, r2 ; get next byte
cpl a ; complement
addc a, #0
mov r2, a
mov a, r3 ; get next byte
cpl a ; complement
addc a, #0
mov r3, a
ret ; done
nah ini revisi yang agak baru dari sang penulis program (W.G.Marshall)
tapi tetep masih belum benar juga.
;====================================================================
; subroutine MUL816
; 8-Bit x 16-Bit to 32-Bit Product signed Multiply
; 2's Complement format
;
; input: r0 = multiplicand X
; r3, r2 = multiplier Y
;
; output: r3, r2, r1, r0 = product P = X x Y (r3 = sign extension)
;
; calls: Cr0, Cr2r3, Mr0r3
;
; alters: acc, C, r4, Bits SignX & SignY
;====================================================================
MUL816: push b
acall Cr0 ; 2's comp -> Mag/Sign
acall Cr2r3 ; 2's comp -> Mag/Sign
mov a, r0 ; load X into acc
mov b, r2 ; load Y low into B
mul ab ; multiply
xch a, r0 ; save X*YL low, reload X
mov r4, b ; temp save X*YL high
mov b, r3 ; load Y high into B
mul ab ; multiply
add a, r4 ; add X*YL high and X*YH low
mov r1, a ; save result
clr a
addc a, b ; X*YH high + 0 + carry flag
mov r2, a ; save result
mov r3, #0 ; MS result = 0
acall Mr0r3 ; Mag/Sign -> 2's Comp
pop b
ret
;=================================================================
; subroutine Cr0
; 8-Bit 2's Complement -> magnitude / Sign Bit Conversion
;
; input: r0 = signed byte
;
; output: r0 = magnitude
; Bit SignX = sign (SignX is set if r0 is a negative number)
;
; alters:
;=================================================================
Cr0: xch a, r0 ; read X into accumulator
clr SignX ; clear sign flag
jnb acc.7, Cr0a ; X is negative if bit 7 is 1
cpl a ; X negative, find abs value
inc a ; X = complement(X)+1
setb SignX ; set sign flag
Cr0a: xch a, r0 ; save magnitude
ret
;====================================================================
; subroutine Cr2r3
; 16-Bit 2's Complement -> magnitude / Sign Bit Conversion
;
; input: r3, r2 = signed word
;
; output: r3, r2 = magnitude
; Bit SignY = sign (SignY is set if negative number)
;
; alters: acc, C
;====================================================================
Cr2r3: mov a, r3
clr SignY ; clear sign flag
jnb acc.7, C1a ; high byte negative?
setb SignY ; set sign flag
xch a, r2 ; number is negative
cpl a ; complement
add a, #1 ; and add +1
xch a, r2 ; get MS byte
cpl a ; complement
addc a, #0
mov r3, a
C1a: ret
;====================================================================
; subroutine Mr0r3
; 32-Bit magnitude / Sign Bit -> 2's Complement Conversion
;
; input: r3, r2, r1, r0 = magnitude
; Bits SignX & SignY = sign bits of operands X and Y
; (set if negative)
;
; output: r3, r2, r1, r0 = signed word
;
; alters: acc, C
;====================================================================
Mr0r3: jb SignX, Mr0r3b ; test X sign
jb SignY, Mr0r3a ; test Y sign
ret
Mr0r3b: jnb SignY, Mr0r3a
ret
Mr0r3a: mov a, r0 ; negate number
cpl a ; complement
add a, #1 ; and add +1
mov r0, a
mov a, r1 ; get next byte
cpl a ; complement
addc a, #0
mov r1, a
mov a, r2 ; get next byte
cpl a ; complement
addc a, #0
mov r2, a
mov a, r3 ; get next byte
cpl a ; complement
addc a, #0
mov r3, a
ret ; done
terima kasih
Salam
-
lieur.......ane bantu sundul atas permintaan yg bikin topik
sundul sundul
-
lieur.......ane bantu sundul atas permintaan yg bikin topik
sundul sundul
nuhun :-)
-
para pakar bantuin donk.....om dian,om risam,om juhar, mas herlambang, om busan...& boss ganteng
-
wah sy gagerti asembler, biasanya saya ngeprogram mikrokontroler pake Mikro-C yg lebih mudah :-\ :-\
-
wah sy gagerti asembler, biasanya saya ngeprogram mikrokontroler pake Mikro-C yg lebih mudah :-\ :-\
wah...sayang sekali
saya malah nga ngerti bahasa C, saya ngerti nya cuma Assambler Doang itu pun cuma buat MCS-51 ( maklum pemula banget)
Terim kasih mas purwanto
-
Waduh.. kemana nih para pakar? corong1
-
Lho taunya klo gak bener di uji bagaimana..... aku pernah punya... ide1 coba saya cari lagi cari1 sabar ya.... sambil saya coba dulu. kalkulasi1 entar kalo udah saya coba post di sini. Kalo bisa pakai program C emang lebih gampang sih....
-
Waduh.. kemana nih para pakar? corong1
kalo di microC/microBasic/microPascal ada yg namanya data type ato jenis variabel mulai yg size nya kecil 1bit sampe 32bit
bit 1–bit 0 or 1
sbit 1–bit 0 or 1
byte,char 8–bit 0 .. 255
short 8–bit -127 .. 128
word 16–bit 0 .. 65535
integer 16–bit -32768 .. 32767
longword 32–bit 0 .. 4294967295
longint 32–bit -2147483648 .. 2147483647
float 32–bit ±1.17549435082 * 10-38 .. ±6.80564774407 * 1038
jadi variabel-variabel tersebut bisa lagsung di +, -, * dan /, tanpa harus ada subroutin.... khusus
-
Coba saya jawab ini saya bikin sendiri aja cari-cari ndak ketemu:
sedikit betulkan pertanyaannya ya.... 8 bit x 16 bit hasilnya maximum 24 bit
contohnya kita kalikan aja nilai 8bit terbesar adalah FF dan 16 bit terbesar FFFF
FF x FFFF = FEFF01 nah hasilnya khan 24 bit.....
procedure yg saya bikin ini digunakan mengalikan nilai 16 bit pada DPTR dan nilai 8 BIT pada R0
hasilnya di simpan di R1,R2,R3
jika dptr di isi FFFF
dan r0 di isi FF
maka hasilnya r1=FE
r2=FF
r3=01
mul16x8:
MOV A,R0
MOV B,dpl
MUL AB
MOV r3,A
MOV r2,B
MOV A,R0
MOV B,dpH
MUL AB
ADD A,r2
MOV r2,A
MOV A,#0
ADDC A,B
MOV r1,A
ret
Apakah gini yang dimaksud?
-
Waduh.. kemana nih para pakar? corong1
kalo di microC/microBasic/microPascal ada yg namanya data type ato jenis variabel mulai yg size nya kecil 1bit sampe 32bit
bit 1–bit 0 or 1
sbit 1–bit 0 or 1
byte,char 8–bit 0 .. 255
short 8–bit -127 .. 128
word 16–bit 0 .. 65535
integer 16–bit -32768 .. 32767
longword 32–bit 0 .. 4294967295
longint 32–bit -2147483648 .. 2147483647
float 32–bit ±1.17549435082 * 10-38 .. ±6.80564774407 * 1038
jadi variabel-variabel tersebut bisa lagsung di +, -, * dan /, tanpa harus ada subroutin.... khusus
Wah...enak juga ya, kalo gitu.
Cuma sayangnya saya bisa nya cuma bahasa Assembler doang, itu juga masih belajar, belum mahir :)
-
Coba saya jawab ini saya bikin sendiri aja cari-cari ndak ketemu:
sedikit betulkan pertanyaannya ya.... 8 bit x 16 bit hasilnya maximum 24 bit
contohnya kita kalikan aja nilai 8bit terbesar adalah FF dan 16 bit terbesar FFFF
FF x FFFF = FEFF01 nah hasilnya khan 24 bit.....
procedure yg saya bikin ini digunakan mengalikan nilai 16 bit pada DPTR dan nilai 8 BIT pada R0
hasilnya di simpan di R1,R2,R3
jika dptr di isi FFFF
dan r0 di isi FF
maka hasilnya r1=FE
r2=FF
r3=01
mul16x8:
MOV A,R0
MOV B,dpl
MUL AB
MOV r3,A
MOV r2,B
MOV A,R0
MOV B,dpH
MUL AB
ADD A,r2
MOV r2,A
MOV A,#0
ADDC A,B
MOV r1,A
ret
Apakah gini yang dimaksud?
iya, seperti ini. terima kasih banyak. Mr joe.
Oia , saya nyobanya pake IDE+ simulator buat nulis program MCS-51, nama nya MicroV7.