User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Assembly section within the Software Development category of DaniWeb, a massive community of 397,767 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,481 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Assembly advertiser:
Aug 11th, 2007
Views: 5,051
User can enter an ascii string of up to 48 characters consisting of 1's or 0's, with randomly interspersed whitespace.

IE: 1011111001011110 will be evaulated equally to
[1011][111....cc001cccx01 111hhhh0

Algorithm parses from right to left so will be truncated at 16 position

Purpose is to give all those still programming in 16 bit an example they can learn from and hopefully embellish and post their version
asm Syntax
  1. Compile with NASMW -fbin file.asm -ofile.com
  2.  
  3. BITS 16
  4.  
  5. org 100H
  6.  
  7. jmp Start
  8.  
  9. ;=============================================================================
  10. Buffer db 48 ; Maximum allowable input
  11. times 48 db 0 ; Pad buffer with nulls
  12.  
  13. ;-----------------------------------------------------------------------------
  14. Start mov ah, 9 ; Display String
  15. mov dx, PrmA
  16. int 21H ; Display initial prompt
  17.  
  18. mov dx, Buffer ; Point to input buffer
  19. mov ax, 0C0AH ; Clear keyboard and get string
  20. int 21H
  21.  
  22. ; Determine if user actually enters anything
  23.  
  24. push si
  25. mov si, dx
  26. xor ax, ax
  27. inc si
  28. mov al, [si] ; Get number of characters entered
  29. and ax, ax
  30. jnz .Eval
  31.  
  32. ; Display prompt of no real meaning
  33.  
  34. mov dx, PrmB
  35. mov ah, 9
  36. int 21H
  37. jmp .Fin
  38.  
  39. ; Begin evaluation of string that contains 0's & 1's with any kind of
  40. ; embedded whitespace
  41.  
  42. .Eval push bx
  43. xor bx, bx ; Intermidate result
  44. add si, ax ; Points to last character of string
  45. mov cx, ax ; Countdown to begining of string
  46. mov dx, 1 ; Initial bit mask
  47. std ; Decrement index
  48. inc cx ; Bump for loops functioning
  49.  
  50. ; Loop is structured this way so not to exceed 16 bit limit
  51.  
  52. .Loop dec cx ; Decrement character count
  53. jz .Done
  54.  
  55. lodsb ; Get byte from string
  56. xor al, 30H ; If valid AX will equal 0 or 1
  57. cmp al, 1
  58. ja .Loop ; otherwise it must be whitespace
  59. jnz .L0 ; Don't bother setting bit
  60. or bx, dx
  61. .L0 shl dx, 1 ; Move mask bit to next position
  62. jnc .Loop ; All 16 bits evaluated
  63.  
  64. ; --- NOTE ---
  65. ; Other snippets in this forum will demonstate how to show result
  66. ; in HEX or DECIMAL. At the time of writing these had not been posted yet.
  67.  
  68. ; Feel free to insert an output routine, the only thing I ask you acknowlege
  69. ; what is mine.
  70.  
  71. .Done mov ax, bx ; Move result to AX
  72. pop bx
  73. cld ; Increment index
  74. .Fin pop si
  75. ret
  76. ; ____________________________________________________________________________
  77.  
  78. PrmA db 10, 10, 10, 13, 'Please enter string of 0 & 1'
  79. db 10, 10, 13, 9, '--> $'
  80. PrmB db 10, 10, 13, 'Ok <> I am outta here', 10, 10, 13, '$'
Post Comment

Only community members can submit or comment on code snippets. You must register or log in to contribute.

DaniWeb Marketplace (Sponsored Links)
All times are GMT -4. The time now is 4:12 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC