x86 程序集,未遵循小字节序(或者是吗?)(Linux) [英] x86 assembly, little endianness not being followed(or is it?) (Linux)
问题描述
我是汇编语言编程的新手,我编写了一个小程序来使用 sys_write 系统调用打印整数.这是我的代码:
I am new to assembly language programming and I wrote a small program to print the integer using sys_write system call. Here's my code :
section .data
N: dw 216
chr: dw ,0,0,0,0x0a
section .bss
section .text
global _start
_start:
xor ax, ax
mov ax, word [N]
mov cx, 10
mov ebx,4
shift_while: div cx
add dx, 0x0030
mov word [chr+ebx],dx
sub ebx, 2
xor dx, dx
cmp ax, 0
jne shift_while
call printchar
exit: mov eax, 1
mov ebx, 0
int 80h
printchar: pushad
mov eax, 4
mov ebx, 1
mov ecx, chr
mov edx, 8
int 80h
popad
ret
我有硬编码 216,要打印的数字,我得到了正确的输出.然而让我感到困惑的是mov word [chr+ebx],dx"指令.dx 在第一次迭代中包含 0x0032,因此在地址 [chr+ebx] 处,该值应存储为32 00(十六进制).但是当我使用 gdb 检查 chr 内存时,它显示:
I have hard coded 216, the number to be printed and I am getting the correct output. However what I am bemused by is the "mov word [chr+ebx],dx" instruction. dx contains 0x0032 in the first iteration so at the address [chr+ebx] this value should be stored as 32 00 (hex). But when I examined chr memory using gdb, it showed:
(gdb) x /5hx 0x80490d2
0x80490d2 <chr>: 0x0032 0x0031 0x0036 0x000a
我期望的是 0x3200 0x3100 0x3600 x0a00 并认为我必须做进一步的内存操作才能得到正确的结果.我是不是哪里出了问题.有什么我似乎看不到的东西.我真的很感激这里的一点帮助.这是我在 stackoverflow 上的第一篇文章.
what I expected was 0x3200 0x3100 0x3600 x0a00 and thought I'd have to do further memory manipulation to get the right result. Am I going wrong somewhere with this. Are there things I can't seem to see. I'd really appreciate a little help here. This is my first first post on stackoverflow.
推荐答案
这只是一个表示的东西——从字节的角度来看,你在内存中拥有的是
It's just a representation thing - what you have in memory from a byte-wise perspective is
32 00 31 00 26 00 0a 00
但是当您将其视为 16 位值时,它是
but when you view this as 16 bit values it's
0032 0031 0026 000a
同样,如果您将其视为 32 位值,它将是:
Similarly, if you viewed it as 32 bit values it would be:
00310032 000a0026
这就是小字节序的奇怪之处.;-)
Such is the weirdness of little endianness. ;-)
这篇关于x86 程序集,未遵循小字节序(或者是吗?)(Linux)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!