x86 convert to lower case assembly

后端 未结 3 2025
孤独总比滥情好
孤独总比滥情好 2020-12-12 08:24

This program is to convert a char pointer into lower case. I\'m using Visual Studio 2010.

This is from another question, but much simpler to read and mo

相关标签:
3条回答
  • 2020-12-12 08:43

    The problem is, that the OR operator like many others don't allow two memory or constant parameters. That means: The OR operator can only have following parameters:

    OR register, memory
    OR register, register
    OR register, constant
    

    The second problem is, that the OR has to store the result to a register, not to memory. Thats why you get an access violation, when the brackets are set. If you remove the brackets, the parameters are ok, but you don't write your lowercase letter to memory, what you intend to do. So use another register, to copy the letter to, and then use OR. For example:

    mov eax, 0          ; zero out the result
    mov edi, [token]      ; move the token to search for into EDI 
    MOV ecx, 0
    LOWERCASE_TOKEN:            ;lowercase the token
    MOV ebx, [edi]        ;## Copy the value to another register ##
    OR ebx, 20h         ;## and compare now the register and the memory ##    
    MOV [edi], ebx      ;##Save back the result ##    
    INC ecx    
    CMP [edi+ecx],0    
    JNZ LOWERCASE_TOKEN    
    MOV ecx, 0
    

    That should work^^

    0 讨论(0)
  • 2020-12-12 08:46

    Your code can alter only the first char (or [edi], 20h) - the EDI does not increment.

    EDIT: found this thread with workaround. Try using the 'dl' instead of al.

    ; move the token address to search for into EDI
    ; (not the *token, as would be with mov edi, [token])
    
    mov edi, token      
    
    LOWERCASE_TOKEN:            ;lowercase the token
      mov al, [edi]
      ; check for null-terminator here !
      cmp al, 0
      je GET_OUT
      or al, 20h
      mov dl, al
      mov [edi], dl
      inc edi
    jmp LOWERCASE_TOKEN
    GET_OUT:
    
    0 讨论(0)
  • 2020-12-12 08:56

    I would load the data into a register, manipulate it there, then store the result back to memory.

    int make_lower(char* token) {
        __asm {
            mov edi, token
            jmp short start_loop
        top_loop:   
            or al, 20h
            mov [edi], al
            inc edi
        start_loop:
            mov al, [edi]
            test al, al
            jnz top_loop
        }       
    }
    

    Note, however, that your conversion to upper-case is somewhat flawed. For example, if the input contains any control characters, it will change them to something else -- but they aren't upper case, and what it converts them to won't be lower case.

    0 讨论(0)
提交回复
热议问题