[GNU ASM] Problem mit 'for-Schleife'



  • Hallo,

    Das Programm sollte eig. Abfragen was er wie oft ausgeben soll und das in buf0(was?) und buf1(wie oft?) speichern.
    Der Code sieht wie folgt aus:

    .section .data
    msg0: .ascii "Eingabe: "
    msg1: .ascii "Anzahl: "
    buf0: .ascii "1234" 
    buf1: .long 0x0
    i: .byte 0
    
    .section .text
    .globl _start
    _start:
    	#output
    	mov $4, %eax
    	mov $1, %ebx
    	mov $msg0, %ecx
    	mov $8, %edx
    	int $0x80
    	#input
    	mov $3, %eax
    	mov $0, %ebx
    	mov $buf0, %ecx
    	mov $5, %edx
    	int $0x80
    	#output
    	mov $4, %eax
    	mov $1, %ebx
    	mov $msg1, %ecx
    	mov $7, %edx
    	int $0x80	
    	#input
    	mov $3, %eax
    	mov $0, %ebx
    	mov $buf1, %ecx
    	mov $1, %edx
    	int $0x80
    if:
    	mov $4, %eax
    	mov $1, %ebx
    	mov $buf0, %ecx
    	mov $4, %edx
    	int $0x80
    	#echo buf0 $buf1-times
    	mov $0, %eax
    	mov (i), %bh
    	cmp buf1(, %eax, 1), %bh 
    	inc %bh
    	mov %bh, (i)
    	jle if
    	jg exit
    exit:
    	mov $0, %eax
    	mov $1, %ebx
    	int $0x80
    

    Doch beim Ausführen sieht das jetzt so aus:

    $ ./for 
    Eingabe:for
    Anzahl:4
    for
    Speicherzugriffsfehler
    $ 
    $
    

    (ein '\n' übergibt das Programm noch nach dem segfault)
    Wenn ich raten müsste würde ich sagen das es bei cmp liegt, aber ich weiß nicht warum und hab damit schon ein wenig rumgespielt.

    Es sei erwähnt das ich gerade erst mit ASM angefangen habe.



  • Diese Stelle ist falsch:

    exit:
    	mov $0, %eax
    	mov $1, %ebx
    	int $0x80
    

    Hast Dich bestimmt vertippt 😉
    So muss es sein:

    exit:
    	mov $1, %eax    # exit Systemaufruf
    	mov $0, %ebx    # Fehlercode
    	int $0x80
    

    So sollte sich das Programm ordnungsgemäss beenden 🙂
    Den Fehlercode, den Dein Programm zurückgibt, kannst Du übrigens in der bash mit diesem Kommando abfragen:

    echo $?
    

Anmelden zum Antworten