Home > front end >  Zero out characters with even number of set bits, and reverse string
Zero out characters with even number of set bits, and reverse string

Time:10-18

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.

CodePudding user response:

int bx
mov dl, [bx]
inc bx
mov dl, [bx]
mov al, dl
mov ah, 0
clc
rcr al, 1
adc ah, 0

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 jp and jpe instructions share the same opcode 7Ah.

  • jp jump if parity (PF=1)
  • jpe jump if parity even (PF=1)

The jnp and jpo instructions share the same opcode 7Bh.

  • jnp jump if no parity (PF=0)
  • jpo jump if parity odd (PF=0)

There are many instructions that modify the parity flag. The below code uses the cmp instruction. 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 the jnp instruction.

      ...
011F  mov  cl, [bx]

0121  mov  bx, 202            ; Where the string starts
0124  cmp  byte ptr [bx], 0   ; Have the parity flag defined
0127  jnp  012C               ; Skip in case of no parity
0129  mov  byte ptr [bx], 0   ; Zero in case of parity
012C  inc  bx                 ; Go to next character
012D  loop 0124               ; Until all characters have been processed

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.

012F  mov  byte ptr [bx], "$"

The task of reversing the string requires maintaining two pointers that move towards each other while swapping bytes:

0132  dec  bx          ; Have BX point at the last byte in the string
0133  mov  si, 202     ; Have SI point at the first byte in the string
0136  mov  al, [bx]    ; Read both bytes
0138  mov  dl, [si]
013A  mov  [bx], dl    ; Swap both bytes
013C  mov  [si], al
013E  inc  si          ; Move towards the middle of the string
013F  dec  bx
0140  cmp  si, bx      ; Stop once the pointers cross
0142  jb   0136
  • Related