FizzBuzz/Assembly: Difference between revisions

Undo revision 324439 by Not a robot (talk) / there's already one on the main page, I didn't see it
(Add ARM Assembly)
(Undo revision 324439 by Not a robot (talk) / there's already one on the main page, I didn't see it)
Line 459:
buzz: ;The "buzz" string.
db "buzz"</lang>
 
=={{header|ARM Assembly}}==
This program runs under Linux. To make it run on another system, you would have to rewrite the <code>print</code> routine.
 
<lang>.text
.global _start
_start: mov r8,#100 @ 100 loops
mov r9,#3 @ "Fizz" counter
mov r10,#5 @ "Buzz" counter
mov r11,#'0 @ Decimal representation
mov r12,#'1
1: mov r6,#0 @ R6 will be set if Fizz or Buzz output
subs r9,r9,#1 @ Decrement Fizz counter
moveq r9,#3 @ If zero, reset to 3
moveq r6,#1 @ and set R6
ldreq r1,=fizz @ and print "Fizz"
bleq print
subs r10,r10,#1 @ Decrement Buzz counter
moveq r10,#5 @ If zero, reset to 5
moveq r6,#1 @ and set R6
ldreq r1,=buzz @ and print "Buzz"
bleq print
tst r6,r6 @ If R6 set, don't print number
bne 2f
ldr r1,=num
strb r12,[r1,#-1]! @ Store first digit in string
cmp r11,#'0 @ If the first digit is > 0...
strhib r11,[r1,#-1]! @ ...then store the first one too
bl print
2: ldr r1,=nl @ Finally, print a newline
bl print
add r12,r12,#1 @ Increment the low digit
cmp r12,#'9 @ If that puts it higher than 9...
movhi r12,#'0 @ Then reset it to 0,
addhi r11,r11,#1 @ And increment the high digit instead
subs r8,r8,#1 @ Decrement loop counter
bne 1b @ If not done yet, do next number
mov r0,#0 @ Otherwise, exit to Linux
mov r7,#1
swi #0
print: mov r2,r1 @ Print zero-terminated string on Linux
1: ldrb r0,[r2],#1 @ Get byte and advance R2
tst r0,r0 @ Zero yet?
bne 1b @ If not, keep going
sub r2,r2,r1 @ Calculate length
mov r0,#1 @ STDOUT
mov r7,#4 @ write
push {lr} @ Save the link register first
swi #0
pop {pc}
.data
.align 4
.ascii "**"
num: .asciz ""
fizz: .asciz "Fizz"
buzz: .asciz "Buzz"
nl: .asciz "\n"</lang>
 
=={{header|Z80 Assembly}}==
2,114

edits