字节序 - 有序的时候?

时间:2017-08-16 22:36:58

标签: assembly endianness

我得到了字节序的一般概念,但我对如何以及何时成为的机制有点模糊。我有英特尔x64的这个汇编示例:

section .data

newline_char: db 10
codes: db '0123456789abcdef'

section .text
global _start

print_newline:
    mov rax, 1          ; 'write' syscall identifier
    mov rdi, 1          ; 'stdout' file descriptor
    mov rsi, newline_char   ; starting address of data
    mov rdx, 1          ; number of bytes to write
    syscall
      ret

print_hex:
    mov rax, rdi        ; move input (rdi) to rax
    mov rdi, 1          ; 'stdout' file descriptor
    mov rdx, 1          ; number of bytes to write
    mov rcx, 64         ; bit/position counter

iterate:
    push    rax         ; save input, rax altered later
    sub rcx, 4          ; every iteration shifts 4 fewer
    sar rax, cl         ; shift rax 'cl' times (cl is lowest byte of rcx)
    and rax, 0xf        ; 'AND' rax with 0xf to preserve lowest 4 bits
    lea rsi, [codes + rax]  ; add value of rax to address of codes

    mov rax, 1          ; 'write' syscall identifier (prepare to write)
    push    rcx         ; 'write' syscall alters rcx (counter) (rcx caller saved)

    syscall

    pop rcx         ; restore counter
    pop rax         ; restore original input

    test    rcx, rcx        ; rcx & rcx -> (set flags)
    jnz iterate         ; if not zero, load 'iterate' to RIP

      ret


section .data
demo1: dq 0x1122334455667788
demo2: db 0x10, 0x20, 0x30, 0x40, 0x50, 0x60, 0x70, 0x80

section .text

_start:
    mov rdi, [demo1]
    call    print_hex
    call    print_newline

    mov rdi, [demo2]
    call    print_hex
    call    print_newline

    mov rax, 60
    xor rdi, rdi
    syscall 

当我按原样运行时,我得到11223344556677888070605040302010

所以,我此时有点困惑,因为我会认为8880都会存储在第一个内存地址中。

此时,我想,如果我将标记值保持不变并从64位rax寄存器更改为32位eax寄存器,我将得到{{1} }和11223344。但是,我得到8070605011223344

如果我仍在40302010中使用db存储相同的8个字节,我应该首先获得相同的最低有效字节吗?

而且,为什么不以相反的顺序打印demo2?字节序仅适用于存储的字节吗?

0 个答案:

没有答案