简体   繁体   中英

does the cpu register order matter in assembly?

I'm learning assembly in Linux 32bit . the code bellow converts the uppercase string to lowercase .

I have this strange result, when I change the order of registers in mov instructions . for example if I swap the registers name in these instructions the output disappears .

mov ecx, msg         
mov edx, msglen

it doesn't work if changed to

mov edx, msg 
mov ecx, msglen 

so is it a must to have the registers in this order eax ebx ecx edx ... i'm confused (noob)
- this is the code that works

section .data

msg: db "UPPERCASE", 10  ;  string 
msglen: equ $-msg        ;  string length 

section .bss
section .text
global _start

_start:
    mov ebx, msg  
    mov eax, 9  ; number of iterations equ number of char in str

doloop: 
    add byte  [ebx], 32  ; label doloop
    inc ebx
    dec eax 
    jnz doloop

    mov eax, 4
    mov ebx, 1
    mov ecx, msg
    mov edx, msglen
    int 80h 
    mov eax, 1
    mov ebx, 0 
    int 80h 

Yes and no. Your example is rather poor in the fact that it swaps the meaning of registers (exchanging the values in ECX and EDX ). The Linux kernel requires the inputs to be in registers EBX (the first parameter), ECX (the second), and EDX (the third). Therefore, if you exchange the values in ECX and EDX , you are actually changing the parameter order, telling the system call the wrong information, and you'll undoubtedly get a wrong result.

If, instead, you had simply swapped the order in which you moved data into the registers, nothing would have changed. You can move data into registers in whatever order you want.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM