Download presentation
Presentation is loading. Please wait.
Published bySudirman Hadiman Modified over 6 years ago
1
L13b – 32 bit multiply Department of Electrical and
ECE 2560 L13b – 32 bit multiply Department of Electrical and Computer Engineering The Ohio State University ECE Lecture 1
2
Digital I/O on the 430 32 bit mutiplication
Subroutine for 32 bit addition ECE Lecture 1
3
8 bit addition The MSP 430 has instructions for 8 bit addition - all affect N,Z,C,V status bits - operations are on 8 bits only ADD.B src,dst Add source to destination src+dst dst ADC.B dst Add carry to destination C+dst dst ADDC.B src,dst Add source & carry to dest src+C+dst dst SBC.B dst subtract C’ from destination SUB.B src,dst subtract source from destination SUBC.B src,dst subtract source & C from destination ECE Lecture 1
4
16-bit addition 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. 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 Lecture 1
5
32-bit rotate left Other instructions needed to do x16 multiply resulting in a 32 bit valuie Will use a shift and add routine Need a 32 bit left shift operation use the rla instruction on the lower word use the rlc instruction on the msw Look at these in documentation ECE Lecture 1
6
32 integer operations The MSP 430 does not support 32 bit data
What to do? You the engineer must document the format and create the routines to work on the data. Define 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 Lecture 1
7
The shift and add routine
Will come back to this slide often Put arguments in R5 and R6 Sum will be in R7 srmult mov 2(SP),R5 ;A multiplier mov 4(SP),R6 ;B multiplicand clr R7 ;R7 for sum mov #0x0001,R9 ;the mask for testing mov #8,R ;will execute loop 7 x tol dec R8 jeq done bit R9,R5 ;test bit of multiplier jz nxtbit ;jump if zero add R6,R7 nxbit clrc ;need to do rotate rlc R9 rlc R6 ;multiplicand x2 jmp tol done mov R7,4(SP) ECE Lecture 1
8
32-bit addition Must define this in a subroutine as 32-bit data is not directly support in any instruction. Definition: 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. 0x Would store the 32-bit value 0x ECE Lecture 1
9
A routine to add 32-bit numbers
;data is passed on the stack Will use R4,R5,R6,R7 to do the addition so these must be saved. sra32 push SR push R4 push R5 push R6 push R7 mov 12(SP),R4 ;high byte A mov 14(SP),R5 ;low byte A mov 16(SP),R6 ;high byte B mov 18(SP),R7 ;low byte B add R5,R7 addc R4,R ; R6-R7 is the answer mov R6,16(SP) ;high byte of answer mov R7,18(SP) ;low byte of answer pop R7 pop R6 pop R5 pop R4 pop SR ret ECE Lecture 1
10
Setup for 32-bit multiply
Main routine Arguments to be multiplied at memory arg1 arg2 Result to memory at resx32 Format of number (16 bit dump) in memory arg xhhhh 0xllll arg xhhhh 0xllll res xhhhh 0xllll ECE Lecture 1
11
Code of main The code for the setup and call push arg1h push arg1l
call sr32mult pop R7 pop res32l pop res32h ECE Lecture 1
12
The subroutine The start – should have no side effects
sr32mult push SR push R5 ;for arg1 h multiplier push R6 ;for arg1 l push R7 ;for arg2 h mpcnd push R8 ;for arg2 l push R9 ;for res h push R10 ;for res l push R11 ;for 16 bit mask push R12 ;for loop control push R13 ;for temp ECE Lecture 1
13
The routine start Have saved state Get arguments from stack
mov 28(SP),R5 ;arg1h mov 26(SP),R6 ;arg1l mov (SP),R7 ;arg2h mov 22(SP),R8 ;arg2l clr R9 ;resh clr R ;resl mov #0x0001,R11 ;mask mov #16,R12 ;loop ctl tol dec R12 jeq done ECE Lecture 1
14
The routine shift/add section
tol dec R12 jeq done bit R6,R11 jz nxtbit ;32-bit add push R8 push R7 push R10 push R9 call sra32 pop R13 pop R9 pop R10 nxbit rla R11 rlc R8 rlc R7 jmp tol ECE Lecture 1
15
Then the return code As multiple registers were saved it takes a bit for entry and return done mov R9,xx(SP) mov R10,xx(sp) pop R13 pop R12 pop R11 pop R10 pop R9 pop R8 pop R7 pop R6 pop R5 pop SR ret ECE Lecture 1
16
ECE Lecture 1
17
Subroutine is easy Subroutine is easy as it is just sequential code. No branches or decisions. Main routine must set up arguments on stack, do the call, store the result, clean up the stack. ECE Lecture 1
18
Main routine ;code to test 32-bit add ;set up to call srm32 push T1h
push T1l push T2h push T2l call #sra32 pop R7 pop R1h pop R1l ;set up for 2nd test end jmp end .data T1h .word 0x0000 T1l .word 0xFFFF T2h .word 0x0000 T2l .word 0x0001 R1h .word 0x0000 R1l .word 0x0000 ECE Lecture 1
19
Enter the code assembler
Enter the code and test it. Having a 32 bit add subroutine And shift and add multiply will now work on recursive subroutine call to do factorial. Demo of code with a few values. ECE Lecture 1
20
Could also do a 16-bit multiply
Could also write a routine that does a 16 by 16 multiply, giving a 32-bit result. Call to srmult16 has 2 16-bit values on the top of the stack which is where the 32-bit result is returned. ECE Lecture 1
21
Setup of call to srmult16 push arg1 push arg2 call #srmult16 pop res1h
pop res1l ECE Lecture 1
22
Subroutine srmult16 srmult16 push sr push R5 ;multiplier
push R6 ;multiplicand l push R7 ;multiplicand h push R8 ;sum l push R9 ;sum h push R10 ;mask push R11 ;counter for loop mov ?(SP),R5 mov ?(SP),R6 clr R7 clr R8 clr R9 mov #0x0001,R10 mov #17,R11 ECE Lecture 1
23
Subroutine loop tol dec R11 jeq done bit R10,R5 jz nxbit
;32-bit add – need code add R6,R8 ;brings up subroutine vs code addc R7,R9 nxbit rla R10 ;rotate the mask rla R6 ;multiplcand x2 rlc R7 jmp tol done mov R9,?(SP) ;result h mov R8,?(SP) ;result l ECE Lecture 1
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.