Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/58.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Macos 相对寻址错误-Mac 10.10_Macos_Assembly_Nasm_X86 64 - Fatal编程技术网

Macos 相对寻址错误-Mac 10.10

Macos 相对寻址错误-Mac 10.10,macos,assembly,nasm,x86-64,Macos,Assembly,Nasm,X86 64,我正在努力学习如何编写汇编代码,我是在的帮助下完成的。这是一个很好的资源,我正在尝试用Macho64格式为我的Mac编写64位代码 我在绝对和相对寻址方面遇到了一些麻烦 这是我的代码: DEFAULT REL ;PURPOSE: This program finds the maximum number of a set of data items ; ;VARIABLES: The registers have the following uses ; ; rbx - Hol

我正在努力学习如何编写汇编代码,我是在的帮助下完成的。这是一个很好的资源,我正在尝试用Macho64格式为我的Mac编写64位代码

我在绝对和相对寻址方面遇到了一些麻烦

这是我的代码:

    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
以下是我收到的错误消息:

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
所以我想知道是否有人能帮我。我查找了相对寻址,但我找不到任何简单的语言来解释我做错了什么

我也知道cmp语句是错误的,但我想我可以自己解决。

通常,您应该使用RIP相对寻址来访问单个内存元素。但是,在您的情况下,您正在访问静态阵列(在数据部分/bss部分中分配的阵列)和
如本节中所述

无法使用RIP相对寻址和索引寄存器访问静态阵列

所以当NASM处理代码时

mov rax, [data_items+rbx*4]
它无法执行RIP相对寻址,因此它尝试使用32位绝对+索引地址,这是Mach-O 64位不允许的,这会导致NASM报告错误

Agner手册中的示例3.11b-3.11d介绍了访问静态阵列的三种方法。然而,由于64位OSX不允许32位绝对寻址(尽管在Linux中是可能的),因此第一个示例3.11b是不可能的

示例3.11c使用图像基准参考点
\uuuuuMH\uExecute\uHeader
。我没有研究过这一点,但3.11d很容易理解。使用
lea
将RIP+偏移加载到寄存器中,如下所示:

lea rsi, [rel data_items]
然后使用
mov-rax、[data\u items+rbx*4]
将代码更改为

mov rax, [rsi+rbx*4]

既然您已经删除了
默认的REL
,您应该能够在
[REL data\u items]

中编辑REL,非常感谢。这正是答案。@LordWindy,对于您在第30行出现的错误,请将
cmp 0,rax
更改为
cmp rax,0
。相关:,看起来这个问题是将PGU示例代码从32位Linux移植到64位OS X的又一次尝试。(当您刚开始学习asm时,这不是一项容易的任务!)