CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Oct 2005
    Posts
    6

    My Dollar Sign Problem And Meaning Of Db

    MY DOLLAR SIGN PROBLEM AND MEANING OF DB

    I don't know what's wrong with my code here:

    Code:
    .MODEL TINY
    .CODE
            ORG 0100H
    
    MAIN:
            JMP START
    
            STRING1 DB "Enter Your Name: $"
            STRING2 DB "HELLO$"
            STRING3 DB 30, ?, 29 DUP (?)
    
    START:
            ; =========== SHOW "ENTER YOUR NAME"
            MOV AH, 09H
            MOV DX, OFFSET STRING1
            INT 21H
    
            ; =========== ASK THE NAME
            MOV AH, 0AH
            MOV DX, OFFSET STRING3
            INT 21H
    
            ; =========== SHOW "HELLO"
            MOV AH, 09H
            MOV DX, OFFSET STRING2
            INT 21H
    
            ; =========== THIS IS WHERE THE PROBLEM STARTS
            MOV AH, 09H
            MOV DX, OFFSET STRING3
            INT 21H
    
            ;=========== TERMINATE THE PROGRAM
            INT 20H
            END MAIN
    MAIN
    The output sound be:

    Code:
    Enter Your Name: Paul Mark
    HELLO Paul Mark
    But it prints out gibberish things (much like "memory things"). But if put a dollar sign at the end "Paul Mark$", it will not show that gibberish things but the HELLO would overwrite the "Enter Your Name: " PLUS adding two symbols and then the name.

    1.) Is there any way so that I don't need anymore to put a Dollar Sign at the end when inputting?

    2.) What is that two symbols after the HELLO message?

    3.) Is there any good Debugger that is compatible with Windows XP? Coz I tried to use Turbo Debugger 5.0, but it gave me Windows errors.


    MEANING OF DB

    And this:

    Code:
            STRING3 DB 30, ?, 29 DUP (?)
    Then followed a code like this:

    Code:
            MOV AH, 09H
            MOV DX, OFFSET STRING3
            INT 21H
    But I don't what's the whole "STRING3 DB 30, ?, 29 DUP (?)" means. All I know is that when I increase 30 & 29, my variable size increases.

    4.) Why DB? They said it means Define Byte, but I'm inputting more than one byte.

    5.) Why it needs three things? 30, ?, 29

    6.) What is the meaning of "?"?

    7.) What DUP do? DUP, they said it means DUPLICATE, but what things do I need to duplicate?

    I need also a thorough explanation about DB...

  2. #2
    Join Date
    Nov 2004
    Posts
    34

    Re: My Dollar Sign Problem And Meaning Of Db

    Quote Originally Posted by pmcastillo
    MY DOLLAR SIGN PROBLEM AND MEANING OF DB


    1.) Is there any way so that I don't need anymore to put a Dollar Sign at the end when inputting?

    ...
    STRING3 DB 30, ?, 29 DUP (?)
    I'm not too informed on DOS or TASM (I assume this is TASM code), but it seems to me that the problem is here.

    DOS strings are terminated with a dollar sign and this line above is your input buffer.

    To break down the statement:

    DB 30, - reserves 30 bytes of byte sized data
    ?, - first byte is undefined
    29 DUP (?) - next 29 bytes are undefined (question marks stand for undefined).

    Now what you probably want to do is duplicate 29 dollar signs instead of undefined. I'm not sure the exact TASM syntax for this, I assume it would be something like:

    Code:
    STRING3 DB 30, ?, 29 DUP ("$")
    This will insure your input buffer is '$' terminated. Again, you may need to look up your assembler documents for the exact syntax on this.

    2.) What is that two symbols after the HELLO message?
    May be related to the problem above.

    3.) Is there any good Debugger that is compatible with Windows XP? Coz I tried to use Turbo Debugger 5.0, but it gave me Windows errors.
    I use Olly Debug for all my debugging needs.

    www.ollydbg.de

    I need also a thorough explanation about DB...
    DB, along with DW and DD (for 32 bit versions) allocate memory in whatever section they are defined under. DB is for byte sized data (and all Ascii strings), DW for word size and DD for dword size. The DUP directive allows you to determine the initial data contained in the space.

  3. #3
    Join Date
    Apr 2003
    Posts
    1,755

    Smile Re: My Dollar Sign Problem And Meaning Of Db

    First, your input buffer declaration is somewhat wrong. You should allocate 30 bytes (30 DUP (?)), not 29. It may corrupt 1 byte after it. The buffer structure is

    BYTE 1 = the size of the buffer
    BYTE 2 = the number of chars typed by the user
    BYTE 3 ... = the buffer

    BYTE 1 should be initialized to the size of the buffer. This value determines how many characters the function should accept. Note also that it will accept SIZE - 1 characters. For example, when you specify 30, the function will accept 29 characters only. The last one will contain character 0Dh (the enter character).
    BYTE 2 dont need to be initialized. you can use ? in it or any value. This will be filled with how much character the user typed when DOS functon 0ah returns. Use this to determine how much to display for example.
    BYTE 3 onward is the buffer. You should allocate it with the size you specified in BYTE 1. If for example you placed 30 in byte 1, your buffer should be 30 bytes long.

    Why is it showing gerbish things? It's because you used DOS function 9 wich prints all character until it finds "$" sign. Since your input is not terminated with "$", the function will continue to print the suceeding characters until it finally finds the "$" terminator. It cannot check how many bytes you really need to print.

    Why is it also that the HELLO overwites the Enter your name? When we ask for string input, the <ENTER> (carriage return) moves the cursor to the beginning. If you output string, it will overwrite whatever is in there. To move it to the next line, we usually use 0Dh,0Ah. But we already have the "0Dh", and what we are opnly missing is the '0Ah'. Place it in your STRING2 to make it down 1 line before printing.

    Let's deal with your questions now

    1.) Is there any way so that I don't need anymore to put a Dollar Sign at the end when inputting?

    You can use DOS function 40h (Write To File or Device Using Handle) to write to the standard output (handle 1).

    2.) What is that two symbols after the HELLO message?

    THe symbols you see are the first 2 bytes in your STRING3 declaration. YOu are printing them too. You should use "OFFSET [STRING3 + 2]" to print only what the user has typed.

    3.) Is there any good Debugger that is compatible with Windows XP? Coz I tried to use Turbo Debugger 5.0, but it gave me Windows errors.

    I usually only use DEBUG.EXE in debugging DOS programs. It would be handy when you used to it (maybe).

    4.) Why DB? They said it means Define Byte, but I'm inputting more than one byte.

    In C/C++, you can use "char" to specify strings (eg. char mytext[] = "hello"). It is the same with assembly. You can use DB to specify character array. When you use DB with string, say DB 'HELLO', the compiler converts it to DB 'H', 'E', 'L', 'L', 'O'. The compiler simplifies what you should be doning manually.

    5.) Why it needs three things? 30, ?, 29

    Read the function explanation above

    6.) What is the meaning of "?"?

    It means that you're assigning any value into it. This is used if you don't use the data until it is filled by something inside the code. FOr example, your code uses "?, 29 DUP (?)", the compiler can place any value in it, anyway you fill fill it first with DOS function 0Ah before printing it.

    7.) What DUP do? DUP, they said it means DUPLICATE, but what things do I need to duplicate?

    Maybe I'm missing something in my post in your other thread. It is used to allocate data with same values. When we use "10 DUP (0)", we are directing the compiler to make a 10 copies of character 0. It's used to simplify declaration of data with same values. If, for example, we want to allocate 10 bytes with 0 character but dont use DUP, it would be lengthy to type DB 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.

    Here's the code fix
    Code:
    .MODEL TINY
    .CODE
            ORG 0100H
    
    MAIN:
            JMP START
    
            STRING1 DB "Enter Your Name: $"
            STRING2 DB 0ah, "HELLO $"
            STRING3 DB 30, ?, 30 DUP (0)
    
    START:
            ; =========== SHOW "ENTER YOUR NAME"
            MOV AH, 09H
            MOV DX, OFFSET STRING1
            INT 21H
    
            ; =========== ASK THE NAME
            MOV AH, 0AH
            MOV DX, OFFSET STRING3
            INT 21H
    
            ; =========== SHOW "HELLO"
            MOV AH, 09H
            MOV DX, OFFSET STRING2
            INT 21H
    
            MOV AH, 40H
    	MOV BX, 1
    	XOR CX, CX
    	MOV CL, [STRING3 + 1]
            MOV DX, OFFSET [STRING3 + 2]
            INT 21H
    
            ;=========== TERMINATE THE PROGRAM
            INT 20H
            END MAIN
    MAIN
    Hope it will help you
    Last edited by rxbagain; October 22nd, 2005 at 11:12 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured