FizzBuzz in Assembly - Segmentierungsfehler
Ich versuche, FizzBuzz in Assembly zu schreiben, und sehe die ganze Zeit einen Segmentierungsfehler. Bisher habe ich festgestellt, dass es nicht meine Druckroutinen sind (weil ich deren Inhalt entfernt habe und das Problem weiterhin besteht) und der Fehler sich irgendwo in der Hauptfunktion versteckt.
Ich habe diese Ausgabe erhalten, als ich das Programm ausgeführt habe:
fizzSegmentation fault
Führt mich zu der Annahme, dass es nicht das Problem ist, Division zu verwenden und die Reste nachzuschlagen. Aber ich könnte mich irren, ich habe seit zwei Jahren keinen Zusammenbau mehr gemacht ...
SECTION .data
global _start
fizz: db "fizz", 4
buzz: db "buzz", 4
SECTION .bss
counter: resb 1
SECTION .text
_start:
mov ax,0
mov [counter],ax
main_loop:
cmp ax,100 ;from 0 to 100
je exit ;
mov bl,3 ;divisor
mov ah,0 ;here will be a remainder
div bl ;divide
cmp ah,0 ;compare the remainder with 0
je print_fizz ;print fizz if they equal
mov bl,5 ;new divisor
mov ah,0 ;do I have to do it every time?
div bl ;divide
cmp ah,0 ;compare the remainder with 0
je print_buzz ;print buzz if they equal
jmp print_ax ;print contents of ax if not
inc ax ;increment ax
jmp main_loop ;jump to label
print_ax:
ret
print_fizz:
ret
print_buzz:
ret
exit:
mov rax,1
mov rbx,0
int 80h
ret
Ich kompiliere mit:
yasm -f elf64 -o fizzbuzz.o fizzbuzz.asm
ld -d -o fizzbuzz fizzbuzz.o