Search code examples
stringassemblyx86-16tasm

Replacing some string characters with another one


I am new to assembly language and I am trying to replace the characters *, #, and & with ? but I have no idea what I am doing. How do I replace it? I tried searching for a solution but it's difficult to find one.

K: cmp m3[di], "*"
jb K1
K: cmp m3[di], "&"
jb K1
cmp m3[di], "#"
ja K1
mov m3[di], "?"
K1: inc di
loop K

Solution

  • The code that you posted should not assemble without an error (or at least a warning), because you define the K label twice!

    Your code does indeed loop over the m3 string. That's fine. But when you compare with one of the characters (#, &, *) you should branch using the je (JumpIfEqual) or jne (JumpIfNotEqual) conditional jumps.

    Next is your code applying this:

    K:        cmp  byte ptr m3[di], "#"  ; ASCII 35
              je   Change
              cmp  byte ptr m3[di], "&"  ; ASCII 38
              je   Change
              cmp  byte ptr m3[di], "*"  ; ASCII 42
              jne  NoChange
    Change:   mov  byte ptr m3[di], "?"  ; ASCII 63
    NoChange: inc  di
              loop K
    

    This is a better version that doesn't continually read the same memory byte and replaces the slow loop K instruction by the faster pair dec cx jnz K:

    K:        mov  al, m3[di]
              cmp  al, "#"               ; ASCII 35
              je   Change
              cmp  al, "&"               ; ASCII 38
              je   Change
              cmp  al, "*"               ; ASCII 42
              jne  NoChange
    Change:   mov  byte ptr m3[di], "?"  ; ASCII 63
    NoChange: inc  di
              dec  cx
              jnz  K