内存寻址在x86汇编语言中。

huangapple go评论104阅读模式
英文:

Memory addressing in x86 assembly language

问题

I have the following code that multiplies num1 and num2 and then prints the result (the idea is to print the number 8, which is 56 in ASCII), the 2 questions I would like to ask are marked as comments in the code.

section	.text
	global _start
_start:
    mov ecx, [num1]
    mov eax, [num2]
    mul ecx
    mov [result], eax
    
    mov edx, 1
    mov ecx, result     ; 1. why it doesn't work if I replace the word result with eax.
                        ; 2. why it has to be "result" instead of "[result]" as with the previous
                             lines?
    mov ebx, 1
    mov eax, 4
    int 0x80

    mov eax, 1
    int 0x80

section .data

num1 db 2
num2 db 28

section .bss

result resb 1

For the first question, you need to use "result" instead of "eax" because you want to pass the result of the multiplication to the ecx register, and "eax" contains the result of the multiplication.

For the second question, when moving the value to ecx, you use "result" instead of "[result]" because you want to move the value stored in the memory location referred to by "result," not the address itself.

英文:

I have the following code that multiplies num1 and num2 and then prints the result (the idea is to print the number 8, which is 56 in ascii), the 2 questions I would like to ask are marked as comments in the code.

section	.text
	global _start     
_start:                   
    mov ecx, [num1]
    mov eax, [num2]
    mul ecx
    mov [result], eax  
    
    mov edx, 1
    mov ecx, result     ; 1. why it doesn't work if I replace the word result with eax.
                        ; 2. why it has to be "result" instead of "[result]" as with the previous
                             lines?
    mov	ebx, 1	
    mov	eax, 4	    
    int	0x80   
	
    mov	eax, 1	  
    int	0x80 

section	.data

num1 db 2
num2 db 28

section .bss

result resb 1

For the first question I would like to understand why I need to create an uninitialized variable first (result) to store the value I want to pass to ecx.

答案1

得分: 1

我想了解为什么我需要首先创建一个未初始化的变量(result)。

这不一定需要是未初始化,但无论何时你想要用SYS_WRITE显示某物时,那个东西需要存在于内存中(而不是在寄存器中),正如Margaret所评论的那样。

你错误地用dbresb来定义了你的内存变量num1, num2, result,它们只保留了1个字节(8位),但指令

mov ecx, [num1]
mov eax, [num2]
mul ecx

都在处理4字节(32位)。

在你的section .data中的内存包含了类似02 1C 00 00 00 00...(十六进制表示)的内容。

mov ecx, [num1] ; 将0x00001C02加载到ECX寄存器。
mov eax, [num2] ; 将0x0000001C加载到EAX寄存器。
mul ecx         ; 计算结果为0x00031038存储在EAX中,EDX为0。

你应该使用以下方式之一声明数字为32位:

num1   DD  2
num2   DD 28
result DD  0

或者只加载/存储寄存器的最低8位:

movzx ecx,[num1] ; 将0x00000002加载到ECX寄存器。
movzx eax,[num2] ; 将0x0000001C加载到EAX寄存器。
mul ecx          ; 计算结果为0x00000038存储在EAX中,EDX为0。
mov [result],AL  ; 结果将为0x38 = 56 = '8'。
英文:

I would like to understand why I need to create an uninitialized variable first (result)

It doesn't neccessarily be uninitialized but whenever you want to display something with SYS_WRITE, that something is required to be in memory (not in a register), as Margaret commented.

You have incorrectly defined your memory variables num1, num2, result with db or resb, which reserves 1 byte only (8 bits), but instructions

mov ecx, [num1]
mov eax, [num2]
mul ecx

all work with 4 bytes (32 bits).
Memory in your section .data contains something like 02 1C 00 00 00 00... (in hexadecimal notation).

mov ecx, [num1] ; load 0x00001C02 to ECX.
mov eax, [num2] ; load 0x0000001C to EAX.
mul ecx         ; compute 0x00031038 to EAX and 0 to EDX.  

You should either declare numbers as 32bits with

num1   DD  2
num2   DD 28
result DD  0  

or load/store only the lowest eight bits of registers:

movzx ecx,[num1] ; load 0x00000002 to ECX.
movzx eax,[num2] ; load 0x0000001C to EAX.
mul ecx          ; compute 0x00000038 to EAX and 0 to EDX.
mov [result],AL  ; result will be 0x38 = 56 = '8'.

huangapple
  • 本文由 发表于 2023年4月17日 14:05:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/76032105.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定