Difference between revisions of "MPLab 32bit Multiplication"

From MKWiki
Jump to navigation Jump to search
Line 20: Line 20:
  
 
;define strings that will be displayed as messages to the user while executing the program.
 
;define strings that will be displayed as messages to the user while executing the program.
msg db 10,13,"Enter the first number: $"
+
msg db 10,13,"Enter the First Number: $"
msg1 db 10,13,"Enter the second number: $"
+
msg1 db 10,13,"Enter the Second Number: $"
msg2 db 10,13,"The product (in Hexadecimal) is: $"
+
msg2 db 10,13,"The Product (in Hexadecimal) is: $"
 
</pre>
 
</pre>
  
Line 31: Line 31:
 
</pre>
 
</pre>
 
<pre>
 
<pre>
MOV EBX, 00000000        ; Initialize EBX to 0; EBX will be used to store the sum.
+
; Using DOS interrupt 21H to display the message.
</pre>
+
MOV AH,09           ;  Set AH to 09 (display string function)
<pre>
+
MOV DX,OFFSET msg          ;Load the offset of the message into DX
; Display the "Enter the first number: " message using DOS interrupt 21h (AH=09h).
+
INT 21H        ;Call DOS interrupt 21h to display the message
MOV AH,09
 
MOV DX,OFFSET num1
 
INT 21H
 
</pre>
 
<pre>
 
MOV ECX, 8         ; initialize ECX to 8, to read 8 characters
 
</pre>
 
<pre>
 
AGAIN: MOV AH, 01    ; Read a character from the input device using DOS interrupt 21h (AH=01h).  AGAIN is label for loop.
 
INT 21H
 
CMP AL, 'A'                  ; Check if the character is greater than or equal to 'A'
 
JGE P1                          ; If greater than 'A', jump to P1
 
SUB AL,30H                  ; subtract 30H to convert it to a decimal digit.   
 
JMP P4
 
P1: SUB AL, 37H          ; subtract 37H to convert it to a decimal digit. 
 
P4: SHL EBX, 4            ; Shift the value in EBX left by 4 bits
 
MOV AH,00                 
 
ADD EBX, EAX         
 
LOOP AGAIN         
 
MOV data1, EBX
 
</pre>
 

Revision as of 00:10, 26 September 2023

32-bit Binary Multiplication

1. Define the model and architecture for the assembly code

.model small 
.stack 100H 
.386          ; specifies that the program will use instructions of the 80386 processor (32-bit instructions).

2. Define your data in the data section ( here you can define various data items: variables, constants, strings, arrays)

.data          ; .data directive indicates the beginning of data section 

; DATA1 and DATA2 are 32-bit variables initialized to 0. 
DATA1 dd 00000000H         
DATA2 dd 00000000H

; PROD1 and PROD2 32-bit uninitialized variables.
PROD1 dd ?           
PROD2 dd ?

;define strings that will be displayed as messages to the user while executing the program.
msg db 10,13,"Enter the First Number: $"
msg1 db 10,13,"Enter the Second Number: $"
msg2 db 10,13,"The Product (in Hexadecimal) is: $"

3. Define your instruction in the code section (here you write the assembly instructions that perform computations, control program flow, and interact with data variables and memory)

.code        ; .code directive indicates the beginning of code section 
.startup     ;  the entry point of the program
; Using DOS interrupt 21H to display the message.
MOV AH,09           ;  Set AH to 09 (display string function)
MOV DX,OFFSET msg           ;Load the offset of the message into DX
INT 21H         ;Call DOS interrupt 21h to display the message