FizzBuzz/Assembly: Difference between revisions

m
Fixed syntax highlighting and duplicate headers.
(moved 6502 / 68000 / 8086 Assembly)
m (Fixed syntax highlighting and duplicate headers.)
 
(4 intermediate revisions by 2 users not shown)
Line 1:
{{collection|FizzBuzz}}
 
=={{header|=360 Assembly}}===
<syntaxhighlight lang="asm">FIZZBUZZ CSECT A SECTION OF CODE STARTS HERE, LABEL IT FIZZBUZZ
**********HOUSE KEEPING AREA**********************
USING *,12 FOR THIS PROGRAM WE ARE GOING TO USE REGISTER 12
Line 87:
**********HOUSE KEEPING AREA**********************
SAVE DS 18F
END HELLO </langsyntaxhighlight>
 
 
=={{header|=6502 Assembly}}===
The modulus operation is rather expensive on the 6502,
so a simple counter solution was chosen.
<syntaxhighlight lang="asm"> .lf fzbz6502.lst
.cr 6502
.tf fzbz6502.obj,ap1
Line 171:
Buzz .da #0
;------------------------------------------------------
.en </langsyntaxhighlight>
 
=={{header|=68000 Assembly}}===
This implementation uses two counters instead of divisions for the moduli.
<langsyntaxhighlight lang="68000devpac">;
; FizzBuzz for Motorola 68000 under AmigaOs 2+ by Thorham
;
Line 271:
 
buzz
dc.b "Buzz",0</langsyntaxhighlight>
 
=={{header|8086=8080 Assembly}}===
 
<syntaxhighlight lang="asm">;; CP/M FizzBuzz in 8080 assembly
 
bdos: equ 5 ; CP/M calls
puts: equ 9
 
max: equ 100 ; Amount of lines to print
 
org 100h
mvi d,max
lxi b,0305h ; Fizz and buzz counters
line: lxi h,num + 2 ; Increment the ASCII number
incn: inr m
mov a,m
cpi '9' + 1
jnz print
mvi m,'0'
dcx h
jmp incn
 
print: mvi e,0 ; Mark that we haven't output anything
dcr b ; Time for fizz?
cz fizzo
dcr c ; Time for buzz?
cz buzzo
dcr e ; Output number if nothing else has been printed
jz check
lxi h,num
call outs
check: lxi h,nl ; Output a newline
call outs
dcr d ; More lines?
jnz line
ret
;; "Fizz", and reset the fizz counter
fizzo: lxi h,fizz
mvi b,3
inr e
jmp outs
;; "Buzz", and reset the buzz counter
buzzo: lxi h,buzz
mvi c,5
mvi e,1
;; Output string in HL preserving registers
outs: push b
push d
push h
mvi c,puts
xchg
call bdos
pop h
pop d
pop b
ret
 
;; Strings
fizz: db 'Fizz$'
buzz: db 'Buzz$'
num: db '000$'
nl: db 13, 10, '$'</syntaxhighlight>
 
 
===8086 Assembly===
Assembly programs that output a number on the screen are programmable in two ways: calculating the number in binary to convert it next in ASCII for output,
or keeping the number in Binary Coded Decimal (BCD) notation
Line 304 ⟶ 371:
is a multiple of five, so the number is never displayed,
because it is replaced by the string "buzz".
<langsyntaxhighlight lang="asm"> ; Init the registers
mov dx,03030h ; For easier printing, the number is
;kept in Binary Coded Decimal, in
Line 391 ⟶ 458:
 
buzz: ;The "buzz" string.
db "buzz"</langsyntaxhighlight>
 
===Z80 Assembly===
For the Amstrad CPC (should work with e.g. the built-in assembler in JavaCPC; use <tt>call &4000</tt> to start from BASIC):
<syntaxhighlight lang="z80">org &4000 ; put code at memory address 0x4000
wr_char equ &bb5a ; write ASCII character in register A to screen
cursor equ &bb78 ; get cursor position
 
push bc
push de
push hl
 
ld b,100 ; loop from 100 to 1
loop:
 
; check for Fizz condition
ld a,(count3)
dec a
jr nz,next3
push bc
ld b,4
ld de,fizz
printfizz:
ld a,(de)
call wr_char
inc de
djnz printfizz
pop bc
ld a,3
next3:
ld (count3),a
 
; check for Buzz condition
ld a,(count5)
dec a
jr nz,next5
push bc
ld b,4
ld de,buzz
printbuzz:
ld a,(de)
call wr_char
inc de
djnz printbuzz
pop bc
ld a,5
next5:
ld (count5),a
 
; test if cursor is still in first column
; (i.e., no Fizz or Buzz has been printed)
call cursor
ld a,h
dec a
jr nz,skipnum
 
; print number
push bc
ld b,3
ld de,count
loop2:
ld a,(de)
call wr_char
inc de
djnz loop2
pop bc
 
skipnum:
; print carriage return/line feed
ld a,13
call wr_char
ld a,10
call wr_char
 
; increment rightmost digit
ld hl,count+2
inc (hl)
ld a,(hl)
; check if value is 10 (ASCII 58)
; if so, set to 48 (ASCII 0) and increase 10's digit
cp 58
jr nz,noinc
ld a,48
ld (count+2),a
ld (hl),a
dec hl
inc (hl)
 
ld a,(hl)
; check second-to-right digit, if it is 10 (0), carry over to 100's
cp 58
jr nz,noinc
ld a,48
ld (count+1),a
ld (hl),a
dec hl
inc (hl)
 
noinc:
 
djnz loop
 
pop hl
pop de
pop bc
 
; return to BASIC
ret
 
count:
db "001"
 
count3:
db 3
 
count5:
db 5
 
fizz:
db "Fizz"
 
buzz:
db "Buzz"</syntaxhighlight>
9,483

edits