Difference between revisions of "MPLab 32bit Multiplication"
Jump to navigation
Jump to search
| 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 49: | Line 49: | ||
CMP AL,'A' | CMP AL,'A' | ||
| − | ; If the character in AL is greater than or equal to 'A', program jumps to Label L5 | + | ; If the character in AL is greater than or equal to 'A', program jumps to Label L5. |
JGE 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 | + | 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> | ||
Revision as of 01:17, 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