Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Part 0 This is an individual activity and is to be accompanied by screen images

ID: 3843282 • Letter: P

Question

Part 0 This is an individual activity and is to be accompanied by screen images and a report. Multiplication of two signed-binary numbers in 2s complement form can be performed by using and subtraction and a of shift and rotate instructions. Below is the pseudo code for Booth's algorithm, which multiplies two signed-binary numbers This algorithm is typically implemented by a hardware digital circuitry incorporated into an arithmetic logic unit. Additional information to confuse you can be found on the last two pages ofthis assignment. write the assembly code that will implement Booth's algorithm through utilization of resources internal to the processor (avoid using external m due to significant latency introduced). Assemble, link, and debug your assembly program with MASM (or MASM32) and View so that it executes in a user-visible window without errors. Document the testing process to prove the functional correctness of your code. You must clearly explain your code using appropriate documentation aids: readable layout, indentation, sparse commenting etc. Procedure for multiplying two signed word-size bin numbers in 2's complement: Initialize the counter to 16 2. Place the Multiplicand in Bx register. 3. Place the Multiplier in Ax register. 4. Initialize DX to zero DX:AX will hold the result 5. Clear the carry Flag (cF) CIC instruction will do it! 6. Check Least significant Bit (LsB) of Multiplier and CE If LSB of Multiplier CF, do nothing and go to step 8 b. LSB of Multiplier and c 0, subtract Multiplicand from the high word of partial product in Dx:AX c. LSB of Multiplier o and CF 1, add ltiplicand to the high word of DX:AX 8. shift high word in Dx, which holds a signed number (must preserve sign), right into low word of partial product Dx:Ax by one position (LSB DX into Ms B of AX) and the bit shifted out of of the low word should be the new value of cF 9. If counter 0, go to step 11 10. Go to step 6 11 Exit Submit your source code, screen images of the assemblyllinking of your code, and screen images of the code execution.

Explanation / Answer

Since the assembly language to use here was not specified in the question, I have assumed it to be 8086.

Please find the 8086 code as below.

.MODEL SMALL
.STACK 100h
.DATA

; variables declared here.

flag db 0
TEN dw 10
temp dw 0
var1 dw 0
var2 dw 0
Intro db 10,13,10,13,' Booths Algorithm (AH:8-AL:8-AX:16)',10,13,10,13,' 7/7-14-bit Multiplication Q-Key to exit',10,13,10,13,' Bit Patterns',10,13,' ----------------------','$'
Prompt db 10,13,' Please enter (0..127) >> $'
result db 10,13,' Result: $'
stepping db 10,13,' Show Stepping? y/n >> $'
bit db 0
tempSi dw 0
size dw 4
posval db 0
negval db 0

; MACRO definitions

PrintChar MACRO char
   mov ah, 02h
   mov dl, char
   int 21h
ENDM

DispMessage MACRO ms
   mov ah, 09h
   mov dx, OFFSET ms
   int 21h
ENDM

GetChar MACRO
   mov ax, 0
   mov ah, 08h
   int 21h
ENDM

DispChar MACRO
   mov ah, 02h
   add dl, 48
   int 21h
ENDM


; Code starts here.
.CODE

Start:

   mov ax, @data
   mov ds,ax

   mov ah,00h ;Clear screen
   mov al,02h
   int 10h

   DispMessage Intro
  
Input:
   DispMessage Prompt
   mov cx, 0
  
Values:
   GetChar
   cmp al, 13       ; Enter key pressed
   je Check
   inc cx
   mov ah, 0      
   push ax
   mov dx, ax       ; print character
   sub dx, 48
   DispChar

   cmp cx, 4
   jl Values
  
Check:  
   cmp flag, 0
   jne Second
  
First:
   call AtoI
   mov ax, temp
   mov ah, 0
   mov var1, ax
   push var1
  
   ; move cursor over
   mov ah, 03h
   int 10h
   mov ah, 02h
   mov dl, 51
   int 10h
  
   mov flag, 1
   call ItoB
  
   jmp Input

Second:

   call AtoI
   mov ax, temp
   mov ah, 0
   mov var2, ax
   push var2

   ; move cursor over
   mov ah, 03h
   int 10h
   mov ah, 02h
   mov dl, 51
   int 10h

   call ItoB

   mov ah, 02h
   mov dl, 13
   int 21h

; print results
   DispMessage result
   mov ax, var1
   call ItoA
   PrintChar '*'
   mov ax, var2
   call ItoA
   PrintChar '='
   mov ax, var1
   mul var2
   push ax
   call ItoA

   ; printing values
   mov ah, 03h       ; move cursor over
   int 10h
   mov ah, 02h
   mov dl, 51
   int 10h
   mov size, 8
   call ItoB

; Start Calculations

   DispMessage stepping
   GetChar
   cmp al, 'y'
   je ShowStepping
   cmp al, 'Y'
   je ShowStepping
   jmp EOF
  
ShowStepping:
   call Booth
  
   jmp EOF

; Procedures definitions starts here

; value in AX

Booth PROC
   pop si

   mov ah, 03h       ; move cursor over
   int 10h
   mov ah, 02h
   inc dh           ; new line
   mov dl, 51
   int 10h
  
   mov size, 8       ; how many binary bits
   mov cx, 8       ; counter
   mov bit, 0

   mov ax, var2       ; set values of pos & neg
   mov posval, al
   not ax
   inc ax
   mov negval, al

   mov ax, var1       ; put ax on top of stack
   push ax
  
   BoothLoop:

   PrintBits:
       pop ax
       push cx           ; save registers
       push ax
       push ax           ; extra copy for printing

       ; printing values
       mov ah, 03h       ; move cursor over
       int 10h
       mov ah, 02h
       inc dh           ; new line
       mov dl, 51
       int 10h

       call ItoB
      
       mov ah, 02h
       mov dl, bit
       add dl, 48
       int 21h

       pop ax           ; restore registers
       pop cx

       cmp bit, 0
       je EvenBit
       jmp OddBit

   EvenBit:
       push ax
       and al, 01h
       cmp al, 0
       jne Add
       jmp ShiftRight

   OddBit:
       push ax
       and al, 01h
       cmp al, 0
       jne Subtract
       jmp ShiftRight
   Add:
       pop ax
       add ah, posval
       push ax

       jmp ShiftRight

   Subtract:
       pop ax
       add ah, negval
       push ax
       jmp ShiftRight

   ShiftRight:
       and al, 1
       mov bit, al

       pop ax           ; shift right 1
       shr ax, 1
       push ax
      
       mov dh, ah       ; keep MSB
       and dh, 80h
       pop ax
       add ah, dh
      
       push ax
       dec cx
   cmp cx, 0
   jne BoothLoop

   push ax
          
   mov ah, 03h       ; move cursor over
   int 10h
   mov ah, 02h
   inc dh           ; new line
   mov dl, 51
   int 10h
   pop ax
   push ax

   call ItoB


   push si
Booth ENDP

;-----------------------------------------------------

AtoI PROC
   pop si
   mov temp, 0
   mov bx, 1
   AtoILoop:
       pop ax
       sub ax, 48
       mul bx
       add temp, ax
      
       ; mul dx by 10
       mov ax, 10
       mul bx
       mov bx, ax

       loop AtoILoop
   push si
   ret
AtoI ENDP
;-----------------------------------------------------
ItoA PROC
   pop si
   ItoALoop:
       mov dx,0
       div TEN ;Product in DX+AX
       push dx ;DX:Remainder
       inc cx
       cmp ax,0
   jne ItoALoop
  
   ItoAPrint:
       pop dx
       add dx, 48
       mov ah, 02h
       int 21h
   loop ItoAPrint
      
   push si
   ret
ItoA ENDP
;-----------------------------------------------------
;   The original value should be on the top of
;   the stack - the integer and not character.

ItoB PROC
   pop tempSi
   pop ax
  
   mov cx, size
   mov dx, 01h       ; dx = 1
   Bin:
       mov dx, 01h
       push ax
       and ax, dx   ; ax = current bit
       pop dx       ; save number from stack
       push ax
       mov ax, dx   ; restore ax
       shr ax, 1   ; go to next bit
   loop Bin

   mov cx, size
   mov bx, 0
PrintB:
   pop dx
   inc bx
   DispChar
   cmp bx, 4
   jne PrintBNext
   mov bx, 0
   mov dl, ' '
   sub dl, 48
   DispChar
PrintBNext:
loop PrintB

   push tempSi
   ret

ItoB ENDP
;-----------------------------------------------------


EOF:
   mov ah, 4ch
   int 21h
   END Start