2017-04-03 25 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