ECE 2560 L 13 b 32 bit multiply
ECE 2560 L 13 b – 32 bit multiply Department of Electrical and Computer Engineering The Ohio State University ECE 3561 - Lecture 1 1
Digital I/O on the 430 l 32 bit mutiplication l Subroutine for 32 bit addition ECE 3561 - Lecture 1 2
8 bit addition l The MSP 430 has instructions for 8 bit addition - all affect N, Z, C, V status bits - operations are on 8 bits only l l ADD. B src, dst Add source to destination src+dst ADC. B dst Add carry to destination C+dst l l ADDC. B src, dst Add source & carry to dest src+C+dst l SBC. B dst l SUB. B src, dst subtract source from destination l SUBC. B src, dst subtract source & C from destination l subtract C’ from destination ECE 3561 - Lecture 1 3
16 -bit addition l l l l The same as on the 8 -bit addition slide except that the operation affects the entire word. The word stored in memory The 16 bits had the more significant byte stored at the higher address and the address of the data must be an even address +1 – msb byte base addr – low byte Memory dump in bytes xx xx ll hh yy yy Memory dump in words xxxx hhll yyyy ECE 3561 - Lecture 1 4
32 -bit rotate left l Other instructions needed to do 16 x 16 multiply resulting in a 32 bit valuie l Will use a shift and add routine l Need a 32 bit left shift operation l use the rla instruction on the lower word l use the rlc instruction on the msw l Look at these in documentation ECE 3561 - Lecture 1 5
32 integer operations The MSP 430 does not support 32 bit data l What to do? l l l Define the data l l l You the engineer must document the format and create the routines to work on the data. high word MSW - stored at base address low word LSW - stored at base + 2 This will allow reading the value in a word display of memory. ECE 3561 - Lecture 1 6
The shift and add routine l l l Will come back to this slide often Put arguments in R 5 and R 6 Sum will be in R 7 l srmult l l l tol l l nxbit l l done mov clr mov dec jeq bit jz add clrc rlc jmp mov 2(SP), R 5 ; A multiplier 4(SP), R 6 ; B multiplicand R 7 ; R 7 for sum #0 x 0001, R 9 ; the mask for testing #8, R 8 ; will execute loop 7 x R 8 done R 9, R 5 ; test bit of multiplier nxtbit ; jump if zero R 6, R 7 ; need to do rotate R 9 R 6 ; multiplicand x 2 tol R 7, 4(SP) l ECE 3561 - Lecture 1 7
32 -bit addition Must define this in a subroutine as 32 -bit data is not directly support in any instruction. l Definition: l l In memory the data is stored in 2 consecutive words. The more significant word is stored at an even address and the least significant word is stored at this address +2. 0 x 0340 0001 4375 Would store the 32 -bit value 0 x 00014375 ECE 3561 - Lecture 1 8
A routine to add 32 -bit numbers l l l l l l ; data is passed on the stack Will use R 4, R 5, R 6, R 7 to do the addition so these must be saved. sra 32 push SR push R 4 push R 5 push R 6 push R 7 mov 12(SP), R 4 ; high byte A mov 14(SP), R 5 ; low byte A mov 16(SP), R 6 ; high byte B mov 18(SP), R 7 ; low byte B add R 5, R 7 addc R 4, R 6 ; R 6 -R 7 is the answer mov R 6, 16(SP) ; high byte of answer mov R 7, 18(SP) ; low byte of answer pop R 7 pop R 6 pop R 5 pop R 4 pop SR ret ECE 3561 - Lecture 1 9
Setup for 32 -bit multiply l Main routine Arguments to be multiplied at memory arg 1 arg 2 l Result to memory at resx 32 l Format of number (16 bit dump) in memory l arg 1 0 xhhhh 0 xllll l arg 2 0 xhhhh 0 xllll l res 32 0 xhhhh 0 xllll l ECE 3561 - Lecture 1 10
Code of main l l l l l The code for the setup and call push arg 1 h push arg 1 l push arg 2 h push arg 2 l call sr 32 mult pop R 7 pop res 32 l pop res 32 h ECE 3561 - Lecture 1 11
The subroutine l l l The start – should have no side effects sr 32 mult push SR push R 5 ; for arg 1 h multiplier push R 6 ; for arg 1 l push R 7 ; for arg 2 h mpcnd push R 8 ; for arg 2 l push R 9 ; for res h push R 10 ; for res l push R 11 ; for 16 bit mask push R 12 ; for loop control push R 13 ; for temp ECE 3561 - Lecture 1 12
The routine start l l l Have saved state Get arguments from stack mov 28(SP), R 5 mov 26(SP), R 6 mov 24(SP), R 7 mov 22(SP), R 8 clr R 9 clr R 10 mov #0 x 0001, R 11 mov #16, R 12 tol dec R 12 jeq done ; arg 1 h ; arg 1 l ; arg 2 h ; arg 2 l ; resh ; resl ; mask ; loop ctl ECE 3561 - Lecture 1 13
The routine shift/add section l l l l l tol nxbit dec R 12 jeq done bit R 6, R 11 jz nxtbit ; 32 -bit add push R 8 push R 7 push R 10 push R 9 call sra 32 pop R 13 pop R 9 pop R 10 rla R 11 rlc R 8 rlc R 7 jmp tol ECE 3561 - Lecture 1 14
Then the return code l l l l As multiple registers were saved it takes a bit for entry and return donemov R 9, xx(SP) mov R 10, xx(sp) pop R 13 pop R 12 pop R 11 pop R 10 pop R 9 pop R 8 pop R 7 pop R 6 pop R 5 pop SR ret ECE 3561 - Lecture 1 15
ECE 3561 - Lecture 1 16
Subroutine is easy l Subroutine is easy as it is just sequential code. No branches or decisions. l Main routine must set up arguments on stack, do the call, store the result, clean up the stack. ECE 3561 - Lecture 1 17
Main routine l l l l l ; code to test 32 -bit add ; set up to call srm 32 push call pop pop nd ; set up for 2 test end jmp. data T 1 h. word T 1 l. word T 2 h. word T 2 l. word R 1 h. word R 1 l. word T 1 h T 1 l T 2 h T 2 l #sra 32 R 7 R 1 h R 1 l end 0 x 0000 0 x. FFFF 0 x 0000 0 x 0001 0 x 0000 ECE 3561 - Lecture 1 18
Enter the code assembler l Enter the code and test it. l Having a 32 bit add subroutine l And shift and add multiply will now work on recursive subroutine call to do factorial. l Demo of code with a few values. ECE 3561 - Lecture 1 19
Could also do a 16 -bit multiply l Could also write a routine that does a 16 by 16 multiply, giving a 32 -bit result. l Call to srmult 16 has 2 16 -bit values on the top of the stack which is where the 32 -bit result is returned. ECE 3561 - Lecture 1 20
Setup of call to srmult 16 l l l push arg 1 push arg 2 call #srmult 16 pop res 1 h pop res 1 l ECE 3561 - Lecture 1 21
Subroutine srmult 16 l l l l srmult 16 push push mov clr clr mov sr R 5 ; multiplier R 6 ; multiplicand l R 7 ; multiplicand h R 8 ; sum l R 9 ; sum h R 10 ; mask R 11 ; counter for loop ? (SP), R 5 ? (SP), R 6 R 7 R 8 R 9 #0 x 0001, R 10 #17, R 11 ECE 3561 - Lecture 1 22
Subroutine loop l l l l tol nxbit done dec R 11 jeq done bit R 10, R 5 jz nxbit ; 32 -bit add – need code add R 6, R 8 ; brings up subroutine vs code addc R 7, R 9 rla R 10 ; rotate the mask rla R 6 ; multiplcand x 2 rlc R 7 jmp tol mov R 9, ? (SP) ; result h mov R 8, ? (SP) ; result l ECE 3561 - Lecture 1 23
- Slides: 23