I am writing this post to share the method with my friends.
I am doing Bachelor in Computer Science and Engineering. My microprocessor course teacher gave us a task to change case from upper or lower to uppercase.
It is kind of "DO it yourself" task.
I am using emu 8086 for this task.
Here are two test cases that we have must pass:
Teas Case 1:
Input : abcd
Output : ABCDTeas Case 2:
Input : aBCd
Output : ABCD
NOTE: Input will be always 4 character and between a-z or A-Z.
We can solve it using conditional instruction.
To make the problem more interesting, I've challenged myself to solve it without branching.
Gladly, I've found a way to solve it using bitwise operator.
Character | ASCII value (binary) |
---|---|
A | 0 1 0 0 0 0 0 1 |
a | 0 1 1 0 0 0 0 1 |
We can see that the 6th bit is zero for uppercase character and one for lowercase character.
If we can set the bit to 0 no matter what is the input, the character will become Uppercase.
We can set it to zero and left rest of bits unchanged by using bitwise AND operation.
a -> 0110 0001 1101 1111 ---------------- AND -> 0100 0001
I am going to use 11011111 (binary) or DF (hex) to set the 6th bit to zero and leave other bits unchanged.
PROC MAIN MOV AH, 01H ; input mode INT 21H ; take 1st character in AL AND AL, 0DFH ; perform AND operation MOV BL, AL ; Move result to another Register INT 21H ; same as 1st character AND AL, 0DFH MOV BH, AL INT 21H ; same as 1st character AND AL, 0DFH MOV CL, AL INT 21H ; same as 1st character AND AL, 0DFH MOV CH, AL MOV AH, 02H MOV DL, 0AH ; print newline INT 21H MOV DL, 0DH ; print cret INT 21H MOV DL, BL ; print 1st character INT 21H MOV DL, BH ; print 2nd character INT 21H MOV DL, CL ; print 3rd character INT 21H MOV DL, CH ; print 4th character INT 21H ENDP
Case 1 :
Case 2 :
Happy Coding !!!
Top comments (0)