Home > Software design >  How to skip spaces at the beginning of the string in assembly x86
How to skip spaces at the beginning of the string in assembly x86

Time:11-11

What loop should I write If I want to skip all spaces at the beginning of the string and start to do something with the string when my code reaches the first symbol of the string. If I have a string like

a='        s o m e word'

the code should start when 's' is reached. It should be some kind of loop but I still don't know how to write it correctly.

My try:

    mov  si, offset buff 2 ;buffer

    mov ah, [si]
    
    loop_skip_space:
        cmp ah,20h ;chech if ah is space            
        jnz increase ;if yes increase si 
        jmp done ;if no end of loop
        increase:
                          
            inc si 

    loop loop_skip_space
    
    done:

CodePudding user response:

  • The mov ah, [si] must be part of the loop. You need to verify different characters from the string, so loading following bytes is necessary.
  • Your code should exit the loop upon finding a space character. Currently you exit upon the first space.
  • The loop instruction requires setting up the CX register with the length of the string. You omitted that.
  mov  si, offset buff 2
  mov  cl, [si-1]         ; I guess this is where the length is, verify this!
  mov  ch, 0
  jcxz done               ; String could start off empty
loop_skip_space:
  cmp  byte ptr [si], ' '
  jne  done
  inc  si                 ; Skip the current space
  loop loop_skip_space
done:

Here SI points at the first non-space character in the string with CX characters remaining. CX could be zero!

CodePudding user response:

look at the example

    STRLEN    EQU  9
    STRING    DB   'Assembler'

              CLD                    ;clear direction flag
              MOV  AL,' '            ;symbol to find.
              MOV  ECX,STRLEN         ;length of string
              LEA  EDI,STRING         ;string pointer.
              REPNE SCASB            ;search itself
              JNZ  K20               ;jump if not found
              DEC  EDI                ;
; EDI points to your first not space char
    K20:      RET
  • Related