Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Many thanks for any assembly-language posts, but can we agree that a FizzBuzz implementation would be more interesting? ;-)


  #include <sys/syscall.h>
  
  modulo:
      udiv x9, x0, x1
      msub x0, x9, x1, x0
      ret
  
  print_fizz:
      mov x0, #0
      adr x1, fizz
      mov x2, #4
      mov x16, SYS_write
      svc #0x80
      ret
  
  print_buzz:
      mov x0, #0
      adr x1, buzz
      mov x2, #4
      mov x16, SYS_write
      svc #0x80
      ret
  
  print_number:
      mov x9, x0
      mov x10, #10
      udiv x11, x0, x10
      mov x0, #0
      adr x1, number_table
      add x1, x1, x11
      mov x2, #1
      svc #0x80
      msub x11, x11, x10, x9
      mov x0, #0
      adr x1, number_table
      add x1, x1, x11
      mov x2, #1
      svc #80
      ret
  
  print_newline:
      mov x0, #0
      adr x1, newline
      mov x2, #1
      mov x16, SYS_write
      svc #0x80
      ret
  
  .globl _main
  .align 2
  _main:
      mov x19, #0
  loop:
      mov x20, #0
      mov x0, x19
      mov x1, #3
      bl modulo
      cmp x0, 0
      cset x20, eq
      b.ne not_3
      bl print_fizz
  not_3:
      mov x0, x19
      mov x1, #5
      bl modulo
      cmp x0, 0
      cset x21, eq
      b.ne not_5
      bl print_buzz
  not_5:
      orr x20, x20, x21
      cmp x20, #0
      b.ne divisible
      mov x0, x19
      bl print_number
  divisible:
      bl print_newline
      add x19, x19, #1
      cmp x19, #100
      b.le loop
      mov x16, #0
      svc #0x80
  
  fizz:
      .ascii "Fizz"
  buzz:
      .ascii "Buzz"
  number_table:
      .ascii "0123456789"
  newline:
      .ascii "\n"




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: