Storing multiple things in a variable?

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

Join Date: Oct 2009
Posts: 27
Reputation: Goalatio is an unknown quantity at this point 
Solved Threads: 0
Goalatio Goalatio is offline Offline
Light Poster

Storing multiple things in a variable?

 
0
  #1
Oct 30th, 2009
So, I was trying to re-create the CMD command "set /p", where it prompts the user to type text, and is ended by a carriage return.

Upon trying this, however, I found that a word (dw) will only hold the last thing passed to it. You can type "abcdefg", but the word will only hold the value "g".

What I'm trying to ask is, would there be a way to make it store everything typed, like.. create a string out of a series of characters?

I'm using NASM16 under a windows operating system.



  1. [org 0100h]
  2. %include "Library.asm"
  3.  
  4. [section .text]
  5.  
  6. START:
  7. input 16 ;Check if it's enter
  8. je ENDLINE ;If equal, end the line
  9. mov [store],al ;Mov AL's value into a variable
  10. string store ;Display the variable
  11. jmp START
  12.  
  13. ENDLINE:
  14. string eol ;End the line
  15. jmp START
  16.  
  17.  
  18.  
  19.  
  20. [section .data]
  21.  
  22. store dw 0, "$"
  23. eol db " ", 13, 10, "$"
It's passion that drives me.
Reply With Quote Quick reply to this message  
Join Date: May 2004
Posts: 85
Reputation: gusano79 is on a distinguished road 
Solved Threads: 5
gusano79 gusano79 is offline Offline
Junior Poster in Training
 
0
  #2
34 Days Ago
Originally Posted by Goalatio View Post
...I found that a word (dw) will only hold the last thing passed to it. You can type "abcdefg", but the word will only hold the value "g"...
Right. store will always refer to the same memory location, so every time you write to it, whatever value was there is overwritten by the new value.

Originally Posted by Goalatio View Post
...What I'm trying to ask is, would there be a way to make it store everything typed, like.. create a string out of a series of characters?
As long as store only points at a single location, no.

What you want is a series of storage locations, with store pointing at the start... instead of dw 0 , do something like times 100 dw 0 . Then all you need is another variable to hold the address in which to store the next input character. Start it out pointing at store , and every time you read a character, load it into the location at the address of store plus the value of the variable, then increment the variable. That way all of the input characters are saved at different locations, and you have all of them.

Additional thoughts:

You don't have infinite space to keep recording characters, so you'll have to decide how much space to reserve and what happens if someone types more characters before they hit the carriage return.

Instead of defining store as a series of 0 bytes, try filling it with '$' --that way, no matter how many characters get typed in, there's always '$' after what was typed, so you get the string terminator for free. If you do it that way, you'll need one more '$' than your maximum character count; always save one for the end of the string.

Character values are coming in al , an 8-bit register, but you're using dw for storage, which is 16 bits... Maybe your Library.asm is looking for 16-bit characters, but if it isn't, consider db instead.
--smg
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 137
Reputation: NotNull is an unknown quantity at this point 
Solved Threads: 14
NotNull's Avatar
NotNull NotNull is offline Offline
Junior Poster
 
0
  #3
33 Days Ago
As to storing multiple things in a variable,
a word is really two contiguous bytes:
  1. albl dw 0
  2. ;same as
  3. albl2 db 0, 0
and can hold two asci values.
If we reserve room for an array of bytes:
  1. albl2 times 64 db 0
albl2 is a label which represents a 16-bit offset, and
we can use it to index into the array, or pass
it as a parameter to 21/3F for example:
  1. mov cx, 64
  2. mov dx, albl2
  3. mov bx, 0
  4. mov ah, 0x3f
  5. int 0x21
----------------------------------------------------------
To control a mind violates a man, and all it has been used for is
hurting and afflicting. Nowonder I progam in assembly...
--->Now available http://dotcoding.netai.net/
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 27
Reputation: Goalatio is an unknown quantity at this point 
Solved Threads: 0
Goalatio Goalatio is offline Offline
Light Poster
 
0
  #4
31 Days Ago
Great! You've really been helping me out on my projects, I really appreciate it.
I'll re-post my code once I get this setup correctly, but here is what I have as of now, try using ALT-NUMPAD ascii codes on it, I was surprised that those worked so easily.
As always, I am using my own library.. So you may see a few things you do not recognize.


  1. [org 0100h]
  2. %include "Library.asm"
  3.  
  4. [section .text]
  5.  
  6. START:
  7. input 13 ;Check if it's enter
  8. je ENDLINE ;If equal, end the line
  9. cmp al,1bh ;Check if ESCAPE
  10. je exitall ;If it is, exit with errorlevel of 0
  11. cmp al,8 ;Check if BACKSPACE is pressed
  12. je BACKSPACE ;Display ASCII character 8 if it is
  13. mov [store1],al ;Mov AL's value into a variable
  14. string store1 ;Display the variable
  15. jmp START
  16.  
  17. BACKSPACE:
  18. mov dx,8h ;ASCII char 8 = backspace (Move cursor back one)
  19. mov ah,02h ;Function 02h, display ASCII character
  20. int 21h ;Call DOS to display it
  21. string blank ;Display a " "
  22.  
  23. ;This will move it back one place, for a proper effect
  24. mov dx,8h ;ASCII char 8 = backspace (Move cursor back one)
  25. mov ah,02h ;Function 02h, display ASCII character
  26. int 21h ;Call DOS to display it
  27. jmp START
  28.  
  29.  
  30. ENDLINE:
  31. string endline ;End the line
  32. jmp START
  33.  
  34. exitall:
  35. exit 0 ;Exit with ERRORLEVEL of 0
  36.  
  37.  
  38.  
  39.  
  40. [section .data]
  41.  
  42. store1 db 0, "$"
  43. blank db " ", "$"
  44. endline db " ", 13, 10, "$"

And I suppose it would be of benefit to post my library too.


  1. ;===========================================
  2. ;string - displays the string passed as an argument
  3. ;===========================================
  4. %macro string 1
  5. xor dx,dx
  6. xor ah,ah
  7. mov dx,%1 ;Move the passed string into DX
  8. mov ah,9 ;Function 09h, display string
  9. int 21h ;Call DOS
  10.  
  11. %endmacro
  12. ;===========================================
  13.  
  14.  
  15.  
  16. ;===========================================
  17. ;exit - exits with passed errorlevel
  18. ;===========================================
  19. %macro exit 1
  20. mov AH,4CH ;Terminate process DOS service
  21. mov AL,%1 ;Pass this value back to ERRORLEVEL
  22. int 21H ;Call DOS to exit
  23.  
  24. %endmacro
  25.  
  26.  
  27.  
  28. ;===========================================
  29. ;input - asks for keyboard input and compares al for the key passed
  30. ;===========================================
  31. %macro input 1
  32. mov ah,0 ;Keyboard input function
  33. int 16h ;Call BIOS
  34. cmp al,%1 ;Compare AL for key passed
  35.  
  36. %endmacro
  37.  
  38. ;==========
  39. ;INPUT2 - asks for a keypress WITHOUT comparing AL
  40. %macro keypress 0
  41. mov ah,0 ;Keyboard input function
  42. int 16h ;Call BIOS
  43.  
  44. %endmacro
  45.  
  46.  
  47.  
  48. ;===========================================
  49. ;arg - Gets the argument and passes it to DX
  50. ;===========================================
  51. %macro arg 0
  52. xor bx, bx ;Zero out BX
  53. mov bl, [cs:0x80] ;Get Command Tail Length
  54. mov byte [bx+0x81],'$' ;Place a $ on command tail
  55. mov bx, 0x82 ;place offset of command tail in BX.
  56. mov dx,bx ;Move offset into DX
  57.  
  58. %endmacro
  59.  
  60.  
  61.  
  62.  
  63. ;===========================================
  64. ;write - writes the string passed into DX
  65. ;Caller must pass:
  66. ;String or value into DX
  67. ;===========================================
  68. %macro write 0
  69. mov ah,9 ;Function 9, display string
  70. int 21h ;Call DOS to display it
  71.  
  72. %endmacro
  73.  
  74.  
  75. ;===========================================
  76. ;position - positions cursor
  77. ;===========================================
  78. %macro position 2
  79. mov dl,%1 ;New X parameter
  80. mov dh,%2 ;New Y parameter
  81. mov ah,02h ;VIDEO service 2: position cursor
  82. mov bh,0 ;Stay with display page 0
  83. int 10h ;Call VIDEO
  84.  
  85. %endmacro
Last edited by Goalatio; 31 Days Ago at 7:15 pm.
It's passion that drives me.
Reply With Quote Quick reply to this message  
Reply

Message:



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