Q. Conversion of BCD number to binary using a procedure?
Conversion of BCD number to binary using a procedure.
Algorithm for conversion procedure:
Take a packed BCD digit and separate the two digits of BCD.
Multiply the upper digit by 10 (0Ah)
Add the lower digit to the result of multiplication
The implementation of procedure would be dependent on parameter-passing scheme. Let's exhibit this with the help of three programs.
Program: Use of registers for parameter passing: This program uses AH register for passing the parameter.
We are presuming that data is available in memory location. BCD and result is stored in BIN
; REGISTERS: Uses CS, DS, SS, SP, AX
; PROCEDURES: BCD-BINARY
DATA_SEG SEGMENT
BCD DB 25h ; storage for BCD value
BIN DB? ; Storage for binary value
DATA_SEG ENDS
STACK_SEG SEGMENT STACK
DW 200 DUP (0); stack of 200 words
TOP_STACK LABEL WORD
STACK_SEG ENDS
CODE_SEG SEGMENT
ASSUME CS: CODE_SEG, DS: DATA_SEG, SS: STACK_SEG
START: MOV AX, DATA_SEG ; Initialise data segment
MOV DS, AX ; Using AX register
MOV AX, STACK_SEG ; Initialise stack
MOV SS, AX ; Segment register. Why
; stack?
MOV SP, OFFSET TOP_STACK ; Initialise stack pointer
MOV AH, BCD
CALL BCD_BINARY ; Do the conversion
MOV BIN, AH ; Store the result in the
; Memory
; Remaining program can be put here
; PROCEDURE: BCD_BINARY - Converts BCD numbers to binary.
; INPUT : AH with BCD value
; OUTPUT : AH with binary value
; DESTROYS : AX
BCD_BINARY PROC NEAR
PUSHF ; Save flags
PUSH BX ; and registers used in procedure
PUSH CX ; before starting the conversion
; Do the conversion
MOV BH, AH ; Save copy of BCD in BH
AND BH, 0Fh ; and mask the higher bits. The lower digit
; is in BH
AND AH, 0F0h ; mask the lower bits. The higher digit is in AH
; But in upper 4 bits.
MOV CH, 04 ; so move upper BCD digit to lower
ROR AH, CH ; four bits in AH
MOV AL, AH ; move the digit in AL for multiplication
MOV BH, 0Ah ; put 10 in BH
MUL BH ; Multiply upper BCD digit in AL
; By 0Ah in BH, the result is in AL
MOV AH, AL ; the maximum/ minimum number would not
; exceed 8 bits so move AL to AH
ADD AH, BH ; Add lower BCD digit to MUL result
; End of conversion, binary result in AH
POP CX ; Restore registers
POP BX
POPF RET ; and return to calling program
BCD_BINARY ENDP
CODE_SEG ENDS
END START
Discussion:
The program written above isn't an optimum program since it doesn't use registers minimally. By now you ought to be able to understand this module. The program copies the BCD number from memory to AH register. AH register is used as it is in the procedure. So the contents of AH register are used in calling program in addition to procedure; or in other words have been passed from main to procedure. Result of the subroutine too is passed back to AH register as returned value. So calling program can find the result in AH register.
The benefit of using the registers for passing the parameters is the ease with that they can be handled. The drawback, though, is the limit of parameters which can be passed. For instance one cannot pass an array of 100 elements to a procedure by using registers.