I have written this code, but I only get the reversed string as output. I want the 1's complement of the reversed string too as output. I know how can I get this. When I edit my code and make it ready for 1's complement of reversed string, my reversed string is no more!
Test case:
input: 11010110
Reversed string: 01101011 1's Complement of it: 10010100
My code:
;REVERSING 8-BIT BINARY NO.
.MODEL
.STACK 100H
.DATA
STR DB 'Enter the binary number (max 8-bit) : $'
STR2 DB 0DH,0AH,'REVERSED STRING : $'
STR3 DB 'THE 1S COMPLEMENT OF IT: $'
.CODE
MAIN PROC
MOV AX, @DATA
MOV DS, AX
LEA DX, STR ; display STR
MOV AH, 9
INT 21H
XOR BL, BL ; CLEAR BL
MOV CX, 8 ; loop for 8 times
MOV AH, 1
INPUT:
INT 21H
CMP AL, 0DH ; compare digit with carriage return (ENTER)
JZ END ; jump to END, if carriage return (JUMP IF ZERO)
AND AL, 01H ; convert ascii to decimal code
SHL BL, 1 ; rotate BX to left by 1 bit
OR BL, AL ; set the LSB of BX with input
LOOP INPUT ; jump to INPUT
END:
MOV AL, BL ; copy BL into AL
MOV CX, 8 ; loop for 8 times
LP: ; loop
SHL AL, 1 ; shift AL to left by 1 bit
RCR BL, 1 ; rotate BL right through carry
LOOP LP ; jump to LP
LEA DX, STR2 ; load and display STR2
MOV AH, 9
INT 21H
MOV CX, 8
MOV AH, 2 ; output function
OUTPUT:
SHL BL, 1 ; shift left BL by 1 bit
JNC ZERO ; jump to label ZERO if CF=0
MOV DL, 31H ; set DL=1. DL=0 for 1's compelement.
JMP DISPLAY ; jump to DISPLAY
ZERO:
MOV DL, 30H ; set DL=0. DL=1 for 1's complement.
DISPLAY:
INT 21H ; display digit
LOOP OUTPUT ; output
MOV AH, 4CH
INT 21H
MAIN ENDP
END MAIN
You can use ROL
instead of SHL
on line 52 to keep the reversed value in BL
register. Now you can reuse it later to output the 1's complement value like this:
MOV CX, 8 ; For looping 8 times
MOV AH, 2
_OUTPUT_INVERSE:
ROL BL, 1 ; Rotate left by 1 bit
JC _I_ZERO ; jump to label ZERO if CF=1
MOV DL, 31H ; set DL=1. DL=0 for 1's compelement.
JMP _I_DISPLAY ; jump to DISPLAY
_I_ZERO:
MOV DL, 30H ; set DL=0. DL=1 for 1's complement.
_I_DISPLAY:
INT 21H
LOOP _OUTPUT_INVERSE ; output the inverse
It works because after you have printed the reverse value, the BL
register contains
the 6B
(ie. 01101011
). So, just shifting (or, rotating) the value left by one time
on each loop will give use the inverse value one by one, which is printed as before with
the reverse value.
Full code: (using your style)
;REVERSING 8-BIT BINARY NO.
.MODEL
.STACK 100H
.DATA
STR DB 'Enter the binary number (max 8-bit) : $'
STR2 DB 0DH,0AH,'REVERSED STRING : $'
STR3 DB 0DH,0AH,'THE 1S COMPLEMENT OF IT: $'
.CODE
MAIN PROC
MOV AX, @DATA
MOV DS, AX
LEA DX, STR ; display STR
MOV AH, 9
INT 21H
XOR BL, BL ; CLEAR BL
MOV CX, 8 ; loop for 8 times
MOV AH, 1
INPUT:
INT 21H
CMP AL, 0DH ; compare digit with carriage return (ENTER)
JZ END ; jump to END, if carriage return (JUMP IF ZERO)
AND AL, 01H ; convert ascii to decimal code
SHL BL, 1 ; rotate BX to left by 1 bit
OR BL, AL ; set the LSB of BX with input
LOOP INPUT ; loop INPUT
END:
MOV AL, BL ; copy BL into AL
MOV CX, 8 ; loop for 8 times
LP: ; loop
SHL AL, 1 ; shift AL to left by 1 bit
RCR BL, 1 ; rotate BL right through carry
LOOP LP ; jump to LP
LEA DX, STR2 ; load and display STR2
MOV AH, 9
INT 21H
MOV CX, 8
MOV AH, 2 ; output function
OUTPUT_REVERSE:
ROL BL, 1 ; rotate left BL by 1 bit
JNC ZERO ; jump to label ZERO if CF=0
MOV DL, 31H ; set DL=1. DL=0 for reversing the value.
JMP DISPLAY ; jump to DISPLAY
ZERO:
MOV DL, 30H ; set DL=0. DL=1 for reversing the value.
DISPLAY:
INT 21H ; display digit
LOOP OUTPUT_REVERSE ; output reverse value
LEA DX, STR3 ; load and display STR3
MOV AH, 9
INT 21H
MOV CX, 8
MOV AH, 2 ; output function
OUTPUT_INVERSE:
ROL BL, 1 ; rotate left by 1 bit
JC I_ZERO ; jump to label ZERO if CF=1
MOV DL, 31H ; set DL=1. DL=0 for 1's compelement.
JMP I_DISPLAY ; jump to DISPLAY
I_ZERO:
MOV DL, 30H ; set DL=0. DL=1 for 1's complement.
I_DISPLAY:
INT 21H
LOOP OUTPUT_INVERSE ; output the inverse value
MOV AH, 4CH
INT 21H
MAIN ENDP
END MAIN
Output:
Enter the binary number (max 8-bit) : 11010110
REVERSED STRING : 01101011
THE 1S COMPLEMENT OF IT: 10010100
By the way, you don't need lines 26 and 27 since the loop is running only 8 times.