How quickly can you code this?

Please support our Assembly advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Mar 2005
Posts: 129
Reputation: Evenbit is on a distinguished road 
Solved Threads: 4
Evenbit's Avatar
Evenbit Evenbit is offline Offline
Junior Poster

How quickly can you code this?

 
0
  #1
Sep 6th, 2008
It's a FizzBuzz question designed to "separate the men from the boys", so don't cheat.

Write a program that prints the numbers from 1 to 100. But for multiples of three print “Fizz” instead of the number and for the multiples of five print “Buzz”. For numbers which are multiples of both three and five print “FizzBuzz”.

Nathan.
while (CPU is present) {some assembly required}
Reply With Quote Quick reply to this message  
Join Date: Mar 2005
Posts: 129
Reputation: Evenbit is on a distinguished road 
Solved Threads: 4
Evenbit's Avatar
Evenbit Evenbit is offline Offline
Junior Poster

Re: How quickly can you code this?

 
0
  #2
Sep 9th, 2008
I posted this because I consider it a "trick question" to some extent. As we hone our "problem-solving" skills, it is natural for us to attempt a clever solution. All too often, however, clever means that we spend too much time on the issue and introduce more opportunities for bugs to creep into the code. Sometimes, we don't see the obvious, simple solution. And I believe that this question is really intended to test "basic skills" rather than discover brilliance.

Here is the FizzBuzz article:

http://imranontech.com/2007/01/24/us...o-grok-coding/

Notice that most of the comments at the bottom present solutions using the modulus operation and other tricks? If you attack the issue using the KISS (Keep It Simple Smarty) principle, you'll see that simple subtraction is all that is necessary.

In Ruby:
  1. fizz = 3
  2. buzz = 5
  3. 1.upto( 100 ) do |i|
  4. fizz -= 1
  5. buzz -= 1
  6. if fizz == 0 and buzz == 0
  7. puts "fizzbuzz"
  8. fizz = 3
  9. buzz = 5
  10. elsif fizz == 0
  11. puts "fizz"
  12. fizz = 3
  13. elsif buzz == 0
  14. puts "buzz"
  15. buzz = 5
  16. else
  17. puts i
  18. end
  19. end

Nathan.
while (CPU is present) {some assembly required}
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 199
Reputation: Tight_Coder_Ex is an unknown quantity at this point 
Solved Threads: 14
Tight_Coder_Ex's Avatar
Tight_Coder_Ex Tight_Coder_Ex is offline Offline
Junior Poster

Re: How quickly can you code this?

 
0
  #3
Sep 10th, 2008
Here is my version of FizzBuss and it took me 2 1/2 hrs. I've read these little
blurbs about how this is used to test potential programmers and in fact the
overall method only took me 20 seconds to determine, but utilizing instructions
that are availiable in later CPU's took a little longer.

If there is interest in this thread I look forward to seeing others optimizations
that are tighter than mine (in ASM) and would probably work on my own version
that is better.

  1. ; Written using ML
  2.  
  3. .686P
  4. .model flat, stdcall
  5. option casemap:none
  6.  
  7. include windows.inc
  8. include kernel32.inc
  9. include user32.inc
  10.  
  11. includelib kernel32.lib
  12. includelib user32.lib
  13.  
  14. Main proto
  15. Print proto :DWORD
  16.  
  17. .data
  18.  
  19. Prompt1 db 13, 10, 13, 10, '3 & 5s Multiples', 13, 10
  20. db '----------------', 13, 10, 0
  21. Epilogue db 13, 10, '-- DONE --', 13, 10, 13, 10, 0
  22.  
  23. Prompt2 db 'Fiss', 0
  24. Prompt3 db 'Buzz', 0
  25. CRLF db 13, 10, 0
  26. FMT db '%4d', 0
  27.  
  28. .data?
  29.  
  30. StdOut dd ?
  31.  
  32. .code
  33. ; =============================================================================
  34.  
  35. start: invoke GetStdHandle, STD_OUTPUT_HANDLE
  36. mov StdOut, eax
  37. invoke Print, offset Prompt1
  38. invoke Main
  39. invoke Print, offset Epilogue
  40. ret
  41.  
  42. ;------------------------------------------------------------------------------
  43. Main proc
  44.  
  45. xor eax, eax ; Intiialize working registers
  46. mov ecx, eax
  47. xor cx, 801H
  48.  
  49. L1: inc eax ; bump counter
  50. cmp al, 100
  51. ja Done ; and process till 100
  52.  
  53. invoke Print, offset CRLF ; append CRLF to previous output
  54.  
  55. shl cx, 1 ; Shift mask bits for 3's & 5's
  56. pushfd
  57. btr ecx, 3 ; Test bit 3 and compliment if on
  58. jnc @F ; NC if not divisible by 3
  59.  
  60. invoke Print, offset Prompt2 ; Print prompt 'Fiss'
  61. or cl, 1 ; Reset mask
  62. bts ecx, 31 ; and let remainder of loop know
  63.  
  64. @@: popfd ; Restore flag from previous condition
  65. jnc @F ; and jump if bit hasn't shifted out
  66.  
  67. invoke Print, offset Prompt3
  68. or ecx, 80000800H ; Reset orginal mask for 5's
  69.  
  70. @@: btr ecx, 31 ; Test and reset if 'Fiss' printed
  71. jc L1 ; If bit was on skip printing number
  72.  
  73. ; Print right justifed number that is not divisible by 3 or 5
  74. push eax
  75. push ecx
  76. enter 20h, 0
  77. mov edx, esp
  78. invoke wsprintf, edx, ADDR FMT, eax
  79. invoke Print, esp
  80. leave
  81. pop ecx
  82. pop eax
  83. jmp L1
  84.  
  85. Done: ret ; Done
  86.  
  87. Main endp
  88.  
  89. ; -----------------------------------------------------------------------------
  90. Print proc uses eax ecx lpszText:DWORD
  91.  
  92. LOCAL Written:DWORD
  93.  
  94. invoke lstrlen, lpszText
  95. push NULL
  96. lea ecx, Written
  97. push ecx
  98. push eax
  99. push lpszText
  100. push StdOut
  101. call WriteFile
  102. ret
  103.  
  104. Print endp
  105.  
  106. end start
Reply With Quote Quick reply to this message  
Join Date: Mar 2005
Posts: 129
Reputation: Evenbit is on a distinguished road 
Solved Threads: 4
Evenbit's Avatar
Evenbit Evenbit is offline Offline
Junior Poster

Re: How quickly can you code this?

 
0
  #4
Sep 11th, 2008
The first rule of code optimization is:
Eliminate conditional branches. Use lookup-tables instead.
The second rule of code optimization is:
If possible, remove any loops.
So, keeping to the KISS philosophy, here is my quick code:

  1. ; For Windows:
  2. ; nasm -f win32 -o fizzbuzz.obj fizzbuzz.asm
  3. ; gcc -o fizzbuzz.exe fizzbuzz.obj
  4.  
  5. ; For Linux: (remove all "_" underscores from this source)
  6. ; nasm -f elf -o fizzbuzz.o fizzbuzz.asm
  7. ; gcc -o fizzbuzz fizzbuzz.o
  8.  
  9. section .data
  10.  
  11. one: db '1',10,'2',10,'Fizz',10,'4',10,'Buzz',10,
  12. db 'Fizz',10,'7',10,'8',10,'Fizz',10,'Buzz',10,'',0
  13. two: db '11',10,'Fizz',10,'13',10,'14',10,'FizzBuzz',10,
  14. db '16',10,'17',10,'Fizz',10,'19',10,'Buzz',10,'',0
  15. three: db 'Fizz',10,'22',10,'23',10,'Fizz',10,'Buzz',10,
  16. db '26',10,'Fizz',10,'28',10,'29',10,'FizzBuzz',10,'',0
  17. four: db '31',10,'32',10,'Fizz',10,'34',10,'Buzz',10,
  18. db 'Fizz',10,'37',10,'38',10,'Fizz',10,'Buzz',10,'',0
  19. five: db '41',10,'Fizz',10,'43',10,'44',10,'FizzBuzz',10,
  20. db '46',10,'47',10,'Fizz',10,'49',10,'Buzz',10,'',0
  21. six: db 'Fizz',10,'52',10,'53',10,'Fizz',10,'Buzz',10,
  22. db '56',10,'Fizz',10,'58',10,'59',10,'FizzBuzz',10,'',0
  23. seven: db '61',10,'62',10,'Fizz',10,'64',10,'Buzz',10,
  24. db 'Fizz',10,'67',10,'68',10,'Fizz',10,'Buzz',10,'',0
  25. eight: db '71',10,'Fizz',10,'73',10,'74',10,'FizzBuzz',10,
  26. db '76',10,'77',10,'Fizz',10,'79',10,'Buzz',10,'',0
  27. nine: db 'Fizz',10,'82',10,'83',10,'Fizz',10,'Buzz',10,
  28. db '86',10,'Fizz',10,'88',10,'89',10,'FizzBuzz',10,'',0
  29. ten: db '91',10,'92',10,'Fizz',10,'94',10,'Buzz',10,
  30. db 'Fizz',10,'97',10,'98',10,'Fizz',10,'Buzz',10,'',0
  31.  
  32. section .text
  33.  
  34. global _main
  35. extern _printf
  36.  
  37. _main:
  38. push one
  39. call _printf
  40. add esp, 4
  41. push two
  42. call _printf
  43. add esp, 4
  44. push three
  45. call _printf
  46. add esp, 4
  47. push four
  48. call _printf
  49. add esp, 4
  50. push five
  51. call _printf
  52. add esp, 4
  53. push six
  54. call _printf
  55. add esp, 4
  56. push seven
  57. call _printf
  58. add esp, 4
  59. push eight
  60. call _printf
  61. add esp, 4
  62. push nine
  63. call _printf
  64. add esp, 4
  65. push ten
  66. call _printf
  67. add esp, 4
  68.  
  69. xor eax, eax
  70. ret

Nathan.
Last edited by Evenbit; Sep 11th, 2008 at 12:58 am. Reason: format
while (CPU is present) {some assembly required}
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 199
Reputation: Tight_Coder_Ex is an unknown quantity at this point 
Solved Threads: 14
Tight_Coder_Ex's Avatar
Tight_Coder_Ex Tight_Coder_Ex is offline Offline
Junior Poster

Re: How quickly can you code this?

 
0
  #5
Sep 11th, 2008
Interesting Nathan, however your example renders the computer redundant as you've already done the calculating, therefore a word processor would have been a better choice.

I do agree with your points of optimization and size should be included in there somewhere too, but at the end of the day examples should always exemplify the computers underlying premise, in that it does the computing.

Hypothetically, we are working in an IT department and now charged with modifying our applications to be dynamic in nature. IE: operator selectable range and pairs of factors (30,000 -> 200,000 & 8's and 18's). My application may increase in size to possibly 1500 bytes, yours would be 3.7 megs 2,500 times larger than mine.

I not beginning to suggest my code is the best or even better example, but rather optimization is far more encompassing and if I were to be in an interview and the recruiter was to evaluate my abilities based on "FIZZBUZZ", I would just simply get up and leave the room.

There doesn't seem to be much interest from others, but in the spirit of competition why don't we modify our applications to be dynamic as indicated above, post our examples and maybe if we are lucky we can have others critique them.

Criteria: User defined range
User defined factors to a maximum of 12

Tabbed output ie:

1
2
3 Fizz
4
5 Buzz
6 Fizz
7
8
9 Fizz
10 Buzz
11
12 Fizz
13
14
15 Fizz Buzz


Note: digits should be right justified, formatting is removing spaces I've entered.
Reply With Quote Quick reply to this message  
Join Date: Mar 2005
Posts: 129
Reputation: Evenbit is on a distinguished road 
Solved Threads: 4
Evenbit's Avatar
Evenbit Evenbit is offline Offline
Junior Poster

Re: How quickly can you code this?

 
0
  #6
Sep 21st, 2008
Originally Posted by Tight_Coder_Ex View Post
Interesting Nathan, however your example renders the computer redundant as you've already done the calculating, therefore a word processor would have been a better choice.
Ah, but my example stays within the specs. The question said *nothing* about the data being pre-computed or not:

  1. Write a program that prints the numbers from 1 to 100. But for multiples of three print “Fizz” instead of the number and for the multiples of five print “Buzz”. For numbers which are multiples of both three and five print “FizzBuzz”.

I do agree with your points of optimization and size should be included in there somewhere too, but at the end of the day examples should always exemplify the computers underlying premise, in that it does the computing.
Oh, but it did do the computing. I simply used the output of my previous example (with a small bit of search-n-replace) to write the code for my last example. Making use of "code-writing tools" is also a good skill to possess.

I not beginning to suggest my code is the best or even better example, but rather optimization is far more encompassing and if I were to be in an interview and the recruiter was to evaluate my abilities based on "FIZZBUZZ", I would just simply get up and leave the room.
I imagine they wouldn't be interested in testing your code-optimization skills because they'd already know/expect you have them.

There doesn't seem to be much interest from others, but in the spirit of competition why don't we modify our applications to be dynamic as indicated above, post our examples and maybe if we are lucky we can have others critique them.

Criteria: User defined range
User defined factors to a maximum of 12
Now you have changed the specs!

I am not optimistic about people's interest in optimization. Heck, I once had an idea that list processing (parsing) would be easier and maybe faster if I used "repne scasd" provided all the words in the list were four characters or less. However, modern processors focus on optimizing discrete coded loops -- so the old "string" instructions are sluggish. Most of the time, one will be more "ahead of the ball" by concentrating on design issues like optimal data structures, overall data flow (paying attention to cache hit/miss), and reducing complexity.

Nathan.
while (CPU is present) {some assembly required}
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Assembly Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC