1

I ran into a problem in my program. Basically what i want to do check if there is a space after a dot in a string and if there isn't i add a space right after the dot. However i don't get how to go about this since my buffer is limited size, therefore if i add the space, the last letter of the buffer will be erased? or am i doing this wrong? thank you for the help in advance :) For example: Hello.Hi = Hello. Hi

MOV cx, ax
        MOV si, offset readBuf
        MOV di, offset writeBuf
      work:
        MOV dl, [si]
        CMP dl, '.'
        JE  dot
      increase:
        MOV [di], dl
        INC si
        INC di
        LOOP work
      dot:
        CMP dl+1, ' '
        JNE noSpace
        JMP increase
      noSpace:
Dominykas Ber
  • 49
  • 1
  • 8

1 Answers1

1

There are a few problems with the code. The first one is this line:

    CMP dl+1, ' '

This is adding 1 to the value in dl and comparing that to a space character which is not what you want. What you want is to compare the next character, so you'll have to load it into a register with MOV dl, [si] or similar.

The second problem is algorithm. It's often easiest to start with psuedo-code and then create the assembly language version from that. For example:

  1. load a character
  2. is there room left?
  3. if not, exit
  4. if so, save the char
  5. does char == period?
  6. if not, go to 1
  7. is there room left?
  8. if not, exit
  9. if so, save space char
  10. load a character
  11. does char == space?
  12. if so, go to 1
  13. if not, go to 2

Note that "load a character" means both fetching the character and incrementing si, and "save a character" means both saving the character and incrementing di. Also note that steps 2, 3 and 4 are identical to steps 7, 8 and 9. This suggests a potential for a subroutine or macro so that you only have to write (and debug!) the code once and can use it multiple times.

Edward
  • 6,964
  • 2
  • 29
  • 55