相对寻址错误 - 苹果10.10 [英] Relative Addressing errors - Mac 10.10

查看:285
本文介绍了相对寻址错误 - 苹果10.10的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想学习如何编写汇编code,我用的http://gnu.mirrors.pair.com/savannah/savannah//pgubook/ProgrammingGroundUp-0-8.pdf.这是一个很好的资源,我试着写code作为64位对我的Mac中Macho64格式。

我碰到的一些麻烦绝对和相对寻址。

这是我的code:

 默认REL
;用途:该程序找到一组数据项的最大数目
;;变量:寄存器具有以下用途
;
; RBX - 保存被检查的数据项的索引
; RDI - 最大的数据发现商品
; RAX - 当前数据项
;
;以下存储位置用于:
;
; data_items - 包含项目数据。 A 0是用来终止数据
;全球_main段.data
    data_items:DW 3,67,34,222,45,75,54,34,44,33,22,11,66,0
    ;这些数据项.text段_主要:
    MOV RDI,0;移动0到索引寄存器
    MOV RAX,[data_items + RBX * 4];加载第一个数据字节
    MOV RDI,RAX,因为这是第一个项目,EAX是最大的start_loop:;启动循环
    CMP 0,RAX;检查,看看是否我们已经打到最后
    JE loop_exit
    INC RDI
    MOV RAX,[data_items + RBX * 4]
    CMP RDI,RAX
    JLE start_loop    MOV RDI,RAX
    JMP start_looploop_exit:
    MOV RAX,0x2000001; 1是出口()系统调用
    系统调用

和这些错误消息我得到:

 萨穆埃尔-MBP:启动SAM $化妆
SRC / maximum.s:26:错误:Mach-O的64位格式不支持32位的绝对地址
SRC / maximum.s:30:错误:OP code和操作数的组合无效
SRC / maximum.s:33:错误:Mach-O的64位格式不支持32位的绝对地址

所以我想知道是否有人能帮助我。我抬起头相对寻址,但我无法找到任何简单的语言解释了什么是我做错了。

我知道了CMP说法是错误的一样好,但我想我可以修复自己。


解决方案

<一个href=\"https://stackoverflow.com/questions/13091987/x64-nasm-pushing-memory-addresses-onto-the-stack-call-function/26407526#26407526\">Mach-O 64位不支持32位的绝对地址,因为图像基数大于2 ^ 32 。

通常应使用相对RIP寻址访问单个存储元件。你的情况,但是你访问一个静态数组(在数据段/ BSS段分配的数组)和结果
作为一节中介绍在瓦格纳雾的优化大会64位模式的寻址静态数组手动


  

这是不可能访问静态数组与RIP-相对寻址和索引寄存器。


所以,当NASM处理您的code

  MOV RAX,[data_items + RBX * 4]

它不能做RIP相对寻址所以它会试图到32位绝对+索引地址未被允许与Mach-O的64位导致NASM报告错误。

Exampels 3.11B-3.11d在瓦格纳手册presents三种方式访问​​静态数组。然而,由于64位的OSX不允许32位绝对地址(虽然它可能在Linux)的第一个例子3.11B是不可能的。

例3.11c使用图像基本参考点 __ mh_execute_header 。我没有看过这个,但3.11d是很容易理解。使用 LEA 加载RIP +偏移这样的寄存器:

  LEA RSI,[REL data_items]

,然后用 MOV RAX更改code,[data_items + RBX * 4]

  MOV RAX,[RSI + RBX * 4]

既然你已经delcared 默认REL ,你应该能够ommit在 [REL data_items] 。<的REL / p>

I'm trying to learn how to write assembly code and I'm doing it with the help of http://gnu.mirrors.pair.com/savannah/savannah//pgubook/ProgrammingGroundUp-0-8.pdf. It's an excellent resource and I'm trying to write the code as 64bit for my Mac in Macho64 format.

I've run into some trouble with absolute and relative addressing.

This is my code:

    DEFAULT REL
;PURPOSE:   This program finds the maximum number of a set of data items
;

;VARIABLES: The registers have the following uses
;
;   rbx - Holds the index of the data item being examined
;   rdi - Largest data item found
;   rax - Current data item
;
;   The following memory locations are used:
;
;   data_items - contains the item data. A 0 is used to terminate the data
;

global _main

section .data
    data_items: dw  3,67,34,222,45,75,54,34,44,33,22,11,66,0
    ;These are the data items

section .text

_main:              
    mov rdi, 0          ;move 0 into index register
    mov rax, [data_items+rbx*4] ;load the first data byte
    mov rdi, rax        ;since this is the first item, eax is biggest

start_loop:         ;start loop
    cmp 0, rax          ;check to see if we've hit the end
    je loop_exit
    inc rdi
    mov rax, [data_items+rbx*4]
    cmp rdi, rax
    jle start_loop

    mov rdi,rax
    jmp start_loop    

loop_exit:
    mov rax, 0x2000001          ;1 is the exit() syscall
    syscall

and these are the error messages I get:

Samuels-MBP:Starting sam$ make
src/maximum.s:26: error: Mach-O 64-bit format does not support 32-bit absolute addresses
src/maximum.s:30: error: invalid combination of opcode and operands
src/maximum.s:33: error: Mach-O 64-bit format does not support 32-bit absolute addresses

So I was wondering if anyone can help me. I looked up Relative Addressing, but I can't find anything that explains in simple language what I am doing wrong.

I do know the cmp statement is wrong as well, but I think I can fix that myself.

解决方案

Mach-O 64-bit does not support 32-bit absolute addressing because the image base is greater than 2^32.

Normally you should use RIP relative addressing for accessing a single memory element. In your case however you're accessing a static array (arrays allocated in the data section/bss section) and
as explained in the the section Addressing static arrays in 64 bit mode in Agner Fog's Optimizing Assembly manual.

It is not possible to access static arrays with RIP-relative addressing and an index register.

So when NASM processes your code

mov rax, [data_items+rbx*4]

it can't do RIP relative addressing so it tries to 32-bit absolute + index address which is not allow with Mach-O 64-bit which causes NASM to report the error.

Exampels 3.11b-3.11d In Agner's manual presents three ways to access static arrays. However, since 64-bit OSX does not allow 32bit absolute addressing (though it's possible in Linux) the first example 3.11b is not possible.

Example 3.11c uses the image base reference point __mh_execute_header. I have not looked into this but 3.11d is easy to understand. Use lea to load the RIP+offset into a register like this:

lea rsi, [rel data_items]

And then change your code using mov rax, [data_items+rbx*4] to

mov rax, [rsi+rbx*4]

Since you have delcared DEFAULT REL you should be able to ommit the rel in [rel data_items].

这篇关于相对寻址错误 - 苹果10.10的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆