Assembly Code for Integer Multiplication

Well you all that familiar with C/C++ Programming must know that to multiply value we will use the syntax *, for example a * b, 5 * 4. Computer that has 32 bit register can do this process easily.


Well, back then I got an assignment to create a Assembly program using MCS-51 that is limited to 8 bit memory(max 255) to do the 4 byte multiplication(32 bit integer multiplication in computer). Here is the Assembly Code :

org 00h

byte1 equ 40h
byte2 equ 41h
byte3 equ 42h
byte4 equ 43h
byte1m equ 30h
byte2m equ 31h
byte3m equ 32h
byte4m equ 33h
temp1 equ 48h
temp2 equ 49h
temp3 equ 4ah
temp4 equ 4bh
temp5 equ 4ch
byte1r equ 60h
byte2r equ 61h
byte3r equ 62h
byte4r equ 63h
byte5r equ 64h
byte6r equ 65h
byte7r equ 66h
byte8r equ 67h
ptrbyte equ 68h
ptrres equ 69h
ptrtemp equ 6ah
ctr1 equ 6bh
ctr2 equ 6ch

start: mov r0,#byte1m
mov r1,#temp1
mov ctr1,#4
mov ctr2,#4
mov ptrbyte,#40h
mov ptrres,#60h
mov ptrtemp,#48h
clr c
mov byte1,#11h
mov byte2,#11h
mov byte3,#11h
mov byte4,#11h
mov byte1m,#11h
mov byte2m,#11h
mov byte3m,#11h
mov byte4m,#11h

mplybyte: mov r0,ptrbyte
mov a,@r0 ;a will be the byte that will be processed, start = byte at address 40h
mov r0,#byte1m
mov r1,#temp1
mainprocess: push 0
mov r0,ptrbyte
mov a,@r0
pop 0
mov b,@r0 ;b will the first byte multiplier at address 30h(byte1m)
mul ab
mov r3,a ;save a
mov r4,b ;save b
mov a,@r1 ;get the value of temp byte before that is pointed by r1,start 48h
addc a,r3
mov @r1,a ;store a to address pointed by r1
inc r1 ;add value to point the next temp byte
mov a,@r1
addc a,b
mov @r1,a
inc r0 ;add value of r0 to point the next byte multiplier
djnz ctr2,mainprocess ;counter loop 4 byte

addbyte: mov r0,ptrres ;set r0 to point byte result(start byte1r)
mov ctr2,#5 ;counter for add(5 byte multiplication = 1 byte x 4 byte)
mov r1,#temp1 ;set r1 with the address of first temporary byte, start 48h
addproses: mov a,@r0 ;get the value at address pointed by r0, store in acc
addc a,@r1
mov @r0,a ;store acc to address pointed by r0(start 60h)
inc r0 ;add r0 to point the next address
inc r1 ;add r1 to point the next address
djnz ctr2,addproses ;counter loop for add
nextbyte: inc ptrbyte ;point to next byte
inc ptrres ;shift 1 byte pointer byte result(multiplication properties)
mov ctr2,#4 ;reset parameter
mov temp1,#0
mov temp2,#0
mov temp3,#0
mov temp4,#0
mov temp5,#0
djnz ctr1,mplybyte ;do until 4 byte
end

Well, when I created this code I notice that I'm so lucky not to program like this when I develop my apllication :). Hope you get something from this post.

Best Regards,
Admin

0 comments: