2017-04-03 2 views
1

割り当てのために、ループを使用して各配列要素をユーザー入力によって提供される目標値と比較するプログラムのコードを完成させる必要があります。たとえば、プログラムで数字を入力して番号6を入力すると、このリストの6番目の番号が表示されます。 '7','3','2','1','0','5','6','4','8','9'アセンブリ:リニア検索コードのトラブル

コメントを入力すると入力しましたが、コンパイルしようとするとこのエラーが発生します。

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 al、target'は、ターゲットのアドレスと_AL_を比較します。 'target'にあるものを比較したいので' cmp al、[target] 'にする必要があります。このエラーにより、リンカエラーが発生します。同様に、私はあなたが 'je targetlocation'(データセクションのラベル)を意味するとは思わないのです。おそらく、あなたは「je found」を意味するのでしょうか? (これはコメントが示唆しているものです) –

+0

@MichaelPetchそれは元々働いていましたが、私は目標位置を "je found"に変更したときに出力が「目標値が見つかりませんでした」と言いました。 "jmp found"に変更しましたが、 "Target value was index 0"と表示されます。任意のアイデアをどのようにこれを修正するには?私は別のジャンプ機能を試してみましたが、私が望むものを私に与えてくれません。 – iwasherenotyou

答えて

-1

あなたはx86アーキテクチャ用のコードを書いている問題はgetNextElementje命令であると仮定すると。 je命令は短いジャンプであり、相対アドレスはパラメータとしてのみ期待され、間接ジャンプを行うために使用することはできません。ここで

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

あなたがメモリにalを移動しているとのコメントが書かれています:

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

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

追加する:ここでは、jejmp命令の間の違いを見ることができます反対です。それを修正し、je foundを使用して再試行してください。

を追加しました:

そしてもう一つ: mov [eax+ecx], al ;copy value from asciinums into an 8-bit register ここにあなたがalにデータをロードすることによってeaxレジスタを台無しにしています。次のループでは、メモリからガベージを取得します。

上記のすべての間違いを修正した後、チェックしても問題はありません。

;;;;;;;;;;;;;;;;;;;; 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

関連する問題