2017-04-03 46 views
1

对于任务,我必须完成程序的代码,该程序使用循环将每个数组元素与通过用户输入提供的目标值进行比较。例如,当程序要求我输入一个数字并输入数字6时,它应该给我这个列表中的第六个数字。装配:我的线性搜索代码出现问题

我输入了评论告诉我要输入的内容,但是当我尝试编译它时,我仍然收到这个错误。

main.o: In function `getNextElement': 
main.asm:(.text+0x92): relocation truncated to fit: R_386_8 against '.data' 

谁能告诉我,我做错了什么吗?

;;;;;;;;;;;;;;;;;;;; MACRO DEFINITIONS ;;;;;;;;;;;;;;;;;;;; 
; A macro with two parameters 
; Implements the write system call 
    %macro writestring 2 
     mov eax, 4 ;sys_write system call number 
     mov ebx, 1 ;file descriptor std_out 
     mov ecx, %1 ;message to write from parameter 1 
     mov edx, %2 ;message length from parameter 2 
     int 0x80 
    %endmacro 
; A macro with two parameters 
; Implements the sys_read call 
    %macro read_string 2 
     mov eax, 3 ;sys_write system call number 
     mov ebx, 2 ;file descriptor std_in 
     mov ecx, %1 ;variable/array to hold data, pass by reference in param 1 
     mov edx, %2 ;number of bytes to read passed by value in param 2 
     int 0x80 
    %endmacro 

;;;;;;;;;;;;;;;;;;;; DATA SEGMENT ;;;;;;;;;;;;;;;;;;;; 
section .data 
msg1 db 'Here are the elements: ' 
lenmsg1 equ $-msg1 
msg2 db 'Enter a number to search for: ' 
lenmsg2 equ $-msg2 
msg3 db 'The target value was found at index ' 
lenmsg3 equ $-msg3 
msg4 db 'The target value was NOT found...',0x0a, 0x0d 
lenmsg4 equ $-msg4 
asciinums db '7','3','2','1','0','5','6','4','8','9' 
lenasciinums equ $-asciinums 
crlf db 0x0d, 0x0a 
lencrlf equ $ - crlf     
target db 0x00 
targetlocation db 0x30 

section .text 
    global _start 
_start: 
    writestring msg1, lenmsg1 
    writestring asciinums, lenasciinums 
    writestring crlf, lencrlf 
    writestring msg2, lenmsg2 
    read_string target, 1 
    writestring crlf, lencrlf 
    mov eax, asciinums ;eax holds base address 
    mov ecx, 0   ;ecx is index register 
getNextElement: 
    mov [eax+ecx], al ;copy value from asciinums into an 8-bit register 
    cmp al, target  ;compare the 8-bit register to target value 
    je targetlocation ;jump if equal to the found label 
    inc ecx    ;increment index register 
    cmp ecx, 10   ;compare index register to decimal 10 
    jne getNextElement ;if index register not equal to 10 go to getNextElement 

    writestring msg4, lenmsg4 
    jmp terminate 
found: 
    add [targetlocation], ecx 
    writestring msg3, lenmsg3 
    writestring targetlocation, 1 
    writestring crlf, lencrlf 
terminate: 
    mov eax, 1   ;terminate program 
    int 0x80 

我也知道问题出自于这部分代码。

mov eax, asciinums ;eax holds base address 
    mov ecx, 0   ;ecx is index register 
getNextElement: 
    mov [eax+ecx], al ;copy value from asciinums into an 8-bit register 
    cmp al, target  ;compare the 8-bit register to target value 
    je targetlocation ;jump if equal to the found label 
    inc ecx    ;increment index register 
    cmp ecx, 10   ;compare index register to decimal 10 
    jne getNextElement ;if index register not equal to 10 go to getNextElement 

这是我用来编译的网站。任何帮助将不胜感激。

https://www.tutorialspoint.com/compile_assembly_online.php

+0

'CMP人,target'目标与_AL_的地址进行比较。你想比较'target'是什么,所以它应该是'cmp al,[target]'。此错误会导致您收到链接器错误。我不认为你的意思是'je targetlocation'(这是数据部分的标签)。也许你的意思是'je found'? (这是评论的建议) –

+0

@MichaelPetch它可以工作,但最初当我将目标位置更改为“je found”时,输出告诉我“找不到目标值。”将其更改为“jmp found”,但只是表示“目标值在索引0处找到”。任何想法如何解决这一问题?我尝试了不同的跳跃诱惑,但他们不给我我想要的。 – iwasherenotyou

回答

-1

假设你正在编写代码x86架构的问题是getNextElementje指令。指令是一个短暂跳转,它只将相对地址作为参数,不能用于间接跳转。在这里,您可以看看差别jejmp指令之间:

http://x86.renejeschke.de/html/file_module_x86_id_146.html

http://x86.renejeschke.de/html/file_module_x86_id_147.html

补充:

mov [eax+ecx], al ;copy value from asciinums into an 8-bit register

这里你移动al到内存中,并评论说,对面。修复它,使用je found并再试一次。

补充:

而另一个问题: mov [eax+ecx], al ;copy value from asciinums into an 8-bit register 这里你毁了将数据装入eax寄存器为al。在下一个循环中,您将从内存中获取垃圾。

检查后,修复所有提到的错误,它完美的作品。

;;;;;;;;;;;;;;;;;;;; MACRO DEFINITIONS ;;;;;;;;;;;;;;;;;;;; 
; A macro with two parameters 
; Implements the write system call 
    %macro writestring 2 
     mov eax, 4 ;sys_write system call number 
     mov ebx, 1 ;file descriptor std_out 
     mov ecx, %1 ;message to write from parameter 1 
     mov edx, %2 ;message length from parameter 2 
     int 0x80 
    %endmacro 
; A macro with two parameters 
; Implements the sys_read call 
    %macro read_string 2 
     mov eax, 3 ;sys_write system call number 
     mov ebx, 2 ;file descriptor std_in 
     mov ecx, %1 ;variable/array to hold data, pass by reference in param 1 
     mov edx, %2 ;number of bytes to read passed by value in param 2 
     int 0x80 
    %endmacro 

;;;;;;;;;;;;;;;;;;;; DATA SEGMENT ;;;;;;;;;;;;;;;;;;;; 
section .data 
msg1 db 'Here are the elements: ' 
lenmsg1 equ $-msg1 
msg2 db 'Enter a number to search for: ' 
lenmsg2 equ $-msg2 
msg3 db 'The target value was found at index ' 
lenmsg3 equ $-msg3 
msg4 db 'The target value was NOT found...',0x0a, 0x0d 
lenmsg4 equ $-msg4 
asciinums db '7','3','2','1','0','5','6','4','8','9' 
lenasciinums equ $-asciinums 
crlf db 0x0d, 0x0a 
lencrlf equ $ - crlf     
target db 0x00 
targetlocation db 0x30 

section .text 
    global _start 
_start: 
    writestring msg1, lenmsg1 
    writestring asciinums, lenasciinums 
    writestring crlf, lencrlf 
    writestring msg2, lenmsg2 
    read_string target, 1 
    writestring crlf, lencrlf 
    mov eax, asciinums ;eax holds base address 
    mov ecx, 0   ;ecx is index register 
getNextElement: 
    mov bl, [eax+ecx] ;copy value from asciinums into an 8-bit register 
    cmp bl, [target] ;compare the 8-bit register to target value 
    je found   ;jump if equal to the found label 
    inc ecx    ;increment index register 
    cmp ecx, 10   ;compare index register to decimal 10 
    jne getNextElement ;if index register not equal to 10 go to getNextElement 

    writestring msg4, lenmsg4 
    jmp terminate 
found: 
    add [targetlocation], ecx 
    writestring msg3, lenmsg3 
    writestring targetlocation, 1 
    writestring crlf, lencrlf 
terminate: 
    mov eax, 1   ;terminate program 
    int 0x80 
+0

是的,如果评论有帮助,请不要忘记投票。 – nopasara