Need to remove all non letter elements from a string in assembly

前端 未结 1 956
梦毁少年i
梦毁少年i 2020-12-12 08:01

with writing this assembly code i cant get this option to work with the removing a non character letter. it will either iterate through the string with all the comparisons a

相关标签:
1条回答
  • 2020-12-12 08:28

    First, think of this as copying from one place to the same place; where bytes that need to be ignored are not copied. You need 2 registers for this - one to keep track of where to get the next byte (e.g. maybe esi), and one to keep track of where to store the next byte/letter (e.g. maybe edi).

    Second, your "is it a letter" branches aren't right (e.g. the letter 'A' or the value 0x41 will be less than the letter 'a' or the value 0x61). It needs to be more like:

        cmp al,'A'       ;Is the value too low to be any letter?
        jb .notLetter    ; yes
        cmp al,'z'       ;Is the value too high to be any letter?
        ja .notLetter    ; yes
        cmp al,'Z'       ;Is the value low enough to be a capital letter?
        jbe .isLetter    ; yes
        cmp al,'a'       ;Is the value high enough to be a lower case letter?
        jae .isLetter    ; yes
                         ; no, not a letter
    .notLetter:
    

    For example (NASM):

    ;Inputs:
    ; ecx    Length of original string
    ; esi    Address of original string
    ;
    ;Outputs
    ; edi    Length of new string
    ; ebx    Address of new string
    
    filterString:
        mov edi,esi      ;edi = address to store string (same address as original string)
        mov ebx,esi      ;ebx = address of both strings (used later)
        jecxz .done      ;Do nothing if the original string has zero length
        cld
    
    .nextByte:
        lodsb            ;AL = next byte, ESI incremented
        cmp al,'A'       ;Is the value too low to be any letter?
        jb .doneByte     ; yes, not a letter
        cmp al,'z'       ;Is the value too high to be any letter?
        ja .doneByte     ; yes, not a letter
        cmp al,'Z'       ;Is the value low enough to be a capital letter?
        jbe .isLetter    ; yes, it's a capital letter
        cmp al,'a'       ;Is the value high enough to be a lower case letter?
        jb .doneByte     ; no, not a letter
                         ; yes, it's a lower case letter
    .isLetter:
        stosb            ;Store AL at EDI, and increment EDI
    
    .doneByte:
        loop .nextByte
    .done:
        sub edi,ebx      ;edi = length of new string
        ret
    
    0 讨论(0)
提交回复
热议问题