I am not able to finish given task using DOS Debug:
Every input string symbol, that has even number of bits has to be changed to 0. And then string has to be reversed and printed to the screen.
a200
db 50
a260
db 'Enter string' 0d 0a '$'
a100
mov ah, 09
mov dx, 260
int 21
mov ah, 0a
mov dx, 200
int 21
mov ah, 02
mov dl, 0d
int 21
mov ah, 02
mov dl, 0a
int 21
xor cx, cx
mov bx, 201
mov cl, [bx]
int bx
mov dl, [bx]
inc bx
mov dl, [bx]
mov al, dl
mov ah, 0
clc
rcr al, 1
adc ah, 0
This is how far I was able to get. However, it is not finished. I am not sure if I am going to the right direction.
I have an idea to use perity flag to check if number of bits is even. However, I can't implement it.
Up to reading the length of the user inputted string, your code looks fine, but then it starts to look like you've just thrown some random stuff together!
Your idea to use the parity flag is ok. When a byte has 0, 2, 4, 6, or 8 bits that are set (to 1), the PF will be set. When a byte has 1, 3, 5, or 7 bits that are set (to 1), the PF will be clear.
The x86 instruction set has 4 instructions that allow you to conditionally jump based on the state of the parity flag:
The
jpandjpeinstructions share the same opcode 7Ah.jpjump if parity (PF=1)jpejump if parity even (PF=1)The
jnpandjpoinstructions share the same opcode 7Bh.jnpjump if no parity (PF=0)jpojump if parity odd (PF=0)There are many instructions that modify the parity flag. The below code uses the
cmpinstruction. In your program you want to zero in case of parity, which is equivalent to skip the zeroing in case of no parity. That's why the code uses thejnpinstruction.At the end of the above loop, the BX register points right behind the string. This is a good moment to apply the $-terminator that you will need in order to print your final result.
The task of reversing the string requires maintaining two pointers that move towards each other while swapping bytes:
EDIT 1
This edit deals with the OP's effort to implement the suggested solution.
This is the OP's code that reportedly runs into an infinite loop:
The reasons why this fails are
loopcould now be running a very long time.a200cmp byte ptr [bx], 0you are overwriting the input buffer that you had setup witha200db 50. Keep data and code apart.a200,a250, anda400you are leaving holes in the program. The CPU will try to execute the bytes that happen to exist in these holes, but since they are not instructions there's a high chance this will fail miserably. Look closely at the code I wrote in my answer. Those numbers (011F, 012F, 0132, ...) in the leftmost column are the addresses where the code belongs so the code forms one contiguous block.The whole code with corrections is:
The
wcommand expects the file size in BX:CX. I assume you have checked BX=0 ?EDIT 2
This edit deals with the OP's effort to refine the program so as to exempt the numerical digits from conversion.
This is the OP's code that reportedly crashes:
The problem today is that the jump targets are off by 2.
You have added 5 lines of new code to your program. Together they take up 12 bytes, but the program seems to think it's 14 bytes.
The whole code with corrections is:
Tip1: ASCII codes are unsigned numbers. Therefore you should use the unsigned conditional branch instructions. I've used
jaJumpIfAbove andjaeJumpIfAboveOrEqual instead ofjgJumpIfGreater andjgeJumpIfGreaterOrEqual.Tip2: If you would use AL instead of DL in those few new lines then the program would shorten by another 2 bytes. You could do this as an exercise, but remember that the branch targets would have to change accordingly, again!