zoukankan      html  css  js  c++  java
  • x86---32汇编(3)---内存寻址模式

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

      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

    运行结果:

  • 相关阅读:
    对象和数据绑定的问题
    Qt父窗口设置为桌面
    MIS的趋势必定是围绕机器取代人手,分工越来越细(小餐厅都支持微信自助点餐,结账时就打个折,相当于省了1、2个人手,SQL发明以后,程序员的工作更多了)
    使用开源软件做项目有风险
    开源免费的C/C++网络库(c/c++ sockets library)
    Bash
    sass
    Spire.XLS
    NET Core+Code First+Docker
    实战网络性能优化
  • 原文地址:https://www.cnblogs.com/xuelanga000/p/13197147.html
Copyright © 2011-2022 走看看