Difference between revisions of "MPLab 32bit Multiplication"
Jump to navigation
Jump to search
| (16 intermediate revisions by the same user not shown) | |||
| Line 39: | Line 39: | ||
; Read the First Number and store it into DATA1 | ; Read the First Number and store it into DATA1 | ||
MOV EBX, 0 ;sets the value of the EBX register to zero | MOV EBX, 0 ;sets the value of the EBX register to zero | ||
| − | MOV CX, 8 ;sets the value of the CX register to 8 | + | MOV CX, 8 ;sets the value of the CX register to 8 (CX is used as a loop counter) |
; Using Loop | ; Using Loop | ||
AGAIN: | AGAIN: | ||
| Line 46: | Line 46: | ||
INT 21H | INT 21H | ||
| − | + | ;compares the value in the AL register (the character just read) with the character 'A'. | |
| − | + | CMP AL,'A' | |
| − | + | ||
| − | + | ; If the character in AL is greater than or equal to 'A', program jumps to Label L5. | |
| + | JGE L5 | ||
| + | |||
| + | ; Otherwise it subtracts 30H from the ASCII value of the character. To convert numeric character('0' to '9') to the numerical value (0 to 9) | ||
| + | SUB AL,30H | ||
| + | |||
| + | ;After subtraction program jumps to Label L6 | ||
JMP L6 | JMP L6 | ||
| + | |||
| + | ;If the character in AL is an upper case letter, it subtracts 37H from the ASCII value of the character | ||
L5: SUB AL,37H | L5: SUB AL,37H | ||
| + | |||
| + | ;shift the contents of the EBX register left by 4 bits. | ||
L6: SHL EBX,4 | L6: SHL EBX,4 | ||
| + | |||
| + | ;Add the value in AL (which has been adjusted to be a numerical value) to the low byte (BL) of the EBX register. | ||
ADD BL,AL | ADD BL,AL | ||
| + | |||
| + | ;This is a loop instruction that decrements the CX register and jumps back to the label AGAIN if CX is not zero. | ||
LOOP AGAIN | LOOP AGAIN | ||
| + | |||
| + | ; EBX contents are moved to DATA1 | ||
MOV DATA1, EBX | MOV DATA1, EBX | ||
| + | </pre> | ||
| + | <pre> | ||
| + | ;Read the Second Number and store it into DATA2 | ||
| + | MOV AH,09 | ||
| + | MOV DX, OFFSET msg1 | ||
| + | INT 21H | ||
| + | |||
| + | MOV EBX, 0 | ||
| + | MOV CX, 8 | ||
| + | |||
| + | AGAIN1: | ||
| + | MOV AH,01 | ||
| + | INT 21H | ||
| + | CMP AL,'A' | ||
| + | JGE L7 | ||
| + | SUB AL, 30H | ||
| + | JMP L8 | ||
| + | L7: SUB AL, 37H | ||
| + | L8: SHL EBX, 4 | ||
| + | ADD BL,AL | ||
| + | LOOP AGAIN1 | ||
| + | MOV DATA2, EBX | ||
| + | </pre> | ||
| + | <pre> | ||
| + | ; Multiply DATA1 and DATA2 and store the 64-bit result in PROD1 and PROD2. | ||
| + | MOV EBX,0 | ||
| + | MOV EDX,0 | ||
| + | MOV EAX,0 | ||
| + | MOV EAX,DATA1 | ||
| + | MOV EBX,DATA2 | ||
| + | MUL EBX ; multiplies the values in the EAX and EBX registers and stores the 64-bit result in the EDX:EAX register pair | ||
| + | |||
| + | MOV PROD1,EDX ; moves the high 32 bits of the result (from the EDX register) into the memory location labeled as PROD1. | ||
| + | MOV PROD2,EAX ; moves the low 32 bits of the result (from the EAX register) into the memory location labeled as PROD2. | ||
| + | </pre> | ||
| + | <pre> | ||
| + | ;Display the product | ||
| + | MOV AH,09 | ||
| + | MOV DX,OFFSET msg2 | ||
| + | INT 21H | ||
| + | |||
| + | ;Higher 32-bits of the product | ||
| + | MOV EBX, PROD1 | ||
| + | MOV CX, 8 | ||
| + | AGAIN2: | ||
| + | ROL EBX, 4 ; Rotate the contents of EBX left by 4 bits (one hexadecimal digit) | ||
| + | MOV DL,BL ; Move the lower 8 bits of EBX (BL) into DL | ||
| + | AND DL, 0FH ; Perform a bitwise AND operation with 0FH to isolate the lower 4 bits (a single hexadecimal digit) | ||
| + | CMP DL, 9 ; Compare DL with 9 | ||
| + | JBE L1 ; Jump to label L1 if DL is less than or equal to 9 | ||
| + | ADD DL, 37H ; Convert DL to the ASCII character representation of the hexadecimal digit ('A' to 'F') | ||
| + | |||
| + | MOV AH, 02 ; Display the character on the screen | ||
| + | INT 21H | ||
| + | JMP L2 | ||
| + | |||
| + | L1: ADD DL,30H ; Convert DL to the ASCII character representation of the decimal digit | ||
| + | MOV AH,02 ; Display the character on the screen | ||
| + | INT 21H | ||
| + | L2: LOOP AGAIN2 | ||
| + | |||
| + | ;Lower 32-bits of the Product | ||
| + | MOV EBX, PROD2 | ||
| + | MOV CX, 8 | ||
| + | AGAIN3: | ||
| + | ROL EBX, 4 | ||
| + | MOV DL,BL | ||
| + | AND DL, 0FH | ||
| + | CMP DL,9 | ||
| + | JBE L3 | ||
| + | ADD DL,37H | ||
| + | MOV AH,02 | ||
| + | INT 21H | ||
| + | JMP L4 | ||
| + | L3: ADD DL,30H | ||
| + | MOV AH,02 | ||
| + | INT 21H | ||
| + | L4: LOOP AGAIN3 | ||
| + | |||
| + | END | ||
</pre> | </pre> | ||
Latest revision as of 11:23, 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 MOV DX,OFFSET msg INT 21H
; Read the First Number and store it into DATA1
MOV EBX, 0 ;sets the value of the EBX register to zero
MOV CX, 8 ;sets the value of the CX register to 8 (CX is used as a loop counter)
; Using Loop
AGAIN:
; Using DOS interrupt 21H to read a character
MOV AH, 01
INT 21H
;compares the value in the AL register (the character just read) with the character 'A'.
CMP AL,'A'
; If the character in AL is greater than or equal to 'A', program jumps to Label L5.
JGE L5
; Otherwise it subtracts 30H from the ASCII value of the character. To convert numeric character('0' to '9') to the numerical value (0 to 9)
SUB AL,30H
;After subtraction program jumps to Label L6
JMP L6
;If the character in AL is an upper case letter, it subtracts 37H from the ASCII value of the character
L5: SUB AL,37H
;shift the contents of the EBX register left by 4 bits.
L6: SHL EBX,4
;Add the value in AL (which has been adjusted to be a numerical value) to the low byte (BL) of the EBX register.
ADD BL,AL
;This is a loop instruction that decrements the CX register and jumps back to the label AGAIN if CX is not zero.
LOOP AGAIN
; EBX contents are moved to DATA1
MOV DATA1, EBX
;Read the Second Number and store it into DATA2
MOV AH,09
MOV DX, OFFSET msg1
INT 21H
MOV EBX, 0
MOV CX, 8
AGAIN1:
MOV AH,01
INT 21H
CMP AL,'A'
JGE L7
SUB AL, 30H
JMP L8
L7: SUB AL, 37H
L8: SHL EBX, 4
ADD BL,AL
LOOP AGAIN1
MOV DATA2, EBX
; Multiply DATA1 and DATA2 and store the 64-bit result in PROD1 and PROD2. MOV EBX,0 MOV EDX,0 MOV EAX,0 MOV EAX,DATA1 MOV EBX,DATA2 MUL EBX ; multiplies the values in the EAX and EBX registers and stores the 64-bit result in the EDX:EAX register pair MOV PROD1,EDX ; moves the high 32 bits of the result (from the EDX register) into the memory location labeled as PROD1. MOV PROD2,EAX ; moves the low 32 bits of the result (from the EAX register) into the memory location labeled as PROD2.
;Display the product
MOV AH,09
MOV DX,OFFSET msg2
INT 21H
;Higher 32-bits of the product
MOV EBX, PROD1
MOV CX, 8
AGAIN2:
ROL EBX, 4 ; Rotate the contents of EBX left by 4 bits (one hexadecimal digit)
MOV DL,BL ; Move the lower 8 bits of EBX (BL) into DL
AND DL, 0FH ; Perform a bitwise AND operation with 0FH to isolate the lower 4 bits (a single hexadecimal digit)
CMP DL, 9 ; Compare DL with 9
JBE L1 ; Jump to label L1 if DL is less than or equal to 9
ADD DL, 37H ; Convert DL to the ASCII character representation of the hexadecimal digit ('A' to 'F')
MOV AH, 02 ; Display the character on the screen
INT 21H
JMP L2
L1: ADD DL,30H ; Convert DL to the ASCII character representation of the decimal digit
MOV AH,02 ; Display the character on the screen
INT 21H
L2: LOOP AGAIN2
;Lower 32-bits of the Product
MOV EBX, PROD2
MOV CX, 8
AGAIN3:
ROL EBX, 4
MOV DL,BL
AND DL, 0FH
CMP DL,9
JBE L3
ADD DL,37H
MOV AH,02
INT 21H
JMP L4
L3: ADD DL,30H
MOV AH,02
INT 21H
L4: LOOP AGAIN3
END