x86---32汇编---内存寻址模式

Posted xuelanga000

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了x86---32汇编---内存寻址模式相关的知识,希望对你有一定的参考价值。

  内存寻址在汇编中非常重要,主要有一下几种:

  1.基于寄存器寻址:

  2.基于寄存器+偏置

  3.基于寄存器+索引寄存器;

  4.基于寄存器+scale*索引寄存器;

代码:

技术图片
#include <stdio.h>
#include <tchar.h>

extern "C" int NumFibVals_;
extern "C" int MemoryAddressing_(int i, int *v1, int *v2, int *v3, int *v4);


int _tmain(int argc, _TCHAR* argv[])
{
    
    for (int i = -1; i < NumFibVals_ + 1; ++i) {
        int v1 = -1, v2 = -1, v3 = -1, v4 = -1;
        int rc = MemoryAddressing_(i, &v1, &v2, &v3, &v4);

        printf("i:%2d rc:%2d-", i, rc);
        printf("v1:%5d v2:%5d v3:%5d v4:%5d
", v1, v2, v3, v4);
    }


    return 0;
}
main
技术图片
    .model flat,c
   
; Simple lookup table (.const section data is read only)
    .const
FibVals dword 0,1,1,2,3,5,8,13
        dword 21,34,55,89,144,233,377,610

NumFibVals_ dword ($-FibVals)/sizeof dword
            public NumFibVals_


; extern "C" int MemoryAddressing_(int i, int* v1, int* v2, int* v3, int*v4);
;
; Description: This function demonstrates various addressing
; modes that can be used to access operands in
; memory.
;
; Returns: 0 = error (invalid table index)
; 1 = success


    .code
MemoryAddressing_ proc
    push ebp
    mov ebp,esp
    push ebx
    push esi
    push edi

;Make sure ‘i‘ is valid
    xor eax,eax
    mov ecx,[ebp+8]                    ;ecx=i
    cmp ecx, 0
    jl InvalidIndex                    ;jump if i<0
    cmp ecx,[NumFibVals_]
    jge InvalidIndex                ;jump if i>=NumFibVals_

;Example #1-base register
    mov ebx,offset FibVals            ;ebx=FibVals
    mov esi,[ebp+8]                    ;esi=i
    shl esi,2                        ;esi=i*4
    add ebx,esi                        ;ebx=FibVals+i*4
    mov eax,[ebx]                    ;Load table value
    mov edi,[ebp+12]                
    mov [edi],eax                    ;Save to ‘v1‘

;Example #2-base register+displacement
;esi is used as the base register
    mov esi,[ebp+8]                    ;esi=i
    shl esi,2                        ;esi=i*4
    mov eax,[esi+FibVals]            ;Load table value
    mov edi, [ebp+16]
    mov [edi],eax                    ;Save to ‘v2‘

;Example #3-base register+index register
    mov ebx,offset FibVals            ;ebx=FibVals
    mov esi,[ebp+8]                    ;esi=i
    shl esi,2                        ;esi=i*4
    mov eax,[ebx+esi]                ;Load table value
    mov edi,[ebp+20]
    mov [edi],eax                    ;Save to ‘v3‘

;Example #4-base register+index register*scale factor
    mov ebx,offset FibVals            ;ebx=FibVals
    mov esi,[ebp+8]                    ;esi=i
    mov eax,[ebx+esi*4]                ;Load table value
    mov edi,[ebp+24]
    mov [edi],eax                    ;Save to ‘v4‘
    mov eax,1                        ;Set return code

InvalidIndex:
    pop edi
    pop esi
    pop ebx
    pop ebp
    ret
MemoryAddressing_ endp
    end
adress

运行结果:

技术图片

以上是关于x86---32汇编---内存寻址模式的主要内容,如果未能解决你的问题,请参考以下文章

X86-64 汇编学习1

X86-64 汇编学习1

《Linux内核 核心知识全解析(完)》

#程序媛生活#大二暑假小学期#X86-32汇编语言

x86---32汇编

x86汇编语言