Search code examples
assemblyx86dosmasm

How to reference the start of a variable


I have a string that received from the user. The address of it gets stored into a variable of where it starts. It gets stored as if it were a C-Style String in C/C++. There are two memory locations before the string that holds the maximum size of the array in the first and the second holds how many alphabetic characters are in the string.

How do I reference the beginning? I can get the end, (Variable Name - 1), which is the last character they put in not including the enter key.

I have tried storing the offset of the beginning of the string and then print just the first character to see if it works. I got a upside down u or Union or something of the sorts.

I have tried offsetting the start where the maxsize is stored and adding two to get to the beginning. Nothing has worked for what I have tried.

                .MODEL  SMALL
            .586

            .STACK  100h

            .DATA
            MAXCHAR EQU 80
            GetStruct DB MAXCHAR
            Read DB ?
            Buffer DB MAXCHAR DUP (?)

            Null DB 13
            Space DB 32

            MenuStr  DB "Please enter your full name (First Middle Last): ",'$'
            MenuStr2 DB "Your name is ",'$'

            Counter DW 1


            .CODE
            include PCMAC.INC
            extern PutDec:NEAR
    Main   PROC
            _Begin

            call AskForName
            call IsolateLastName
            call PrintFirstMiddle

            _Exit 0
    Main   ENDP


    AskForName PROC

               _PutStr MenuStr
               _GetStr GetStruct
               _PutCh 10
               ret
    AskForName ENDP

    IsolateLastName PROC

               mov bl, Read
               test bl, bl
               jz Done_LN
               sub bh, bh
               mov di, offset Buffer - 1
               mov cx, 0
    CompSpace:
              mov dl, [bx + di]
              cmp dl, Space
              je SetBreak
              dec bx
              inc Counter
              jne CompSpace

    SetBreak:
            mov [di], '$'
            inc bx

    IsSpace:
            mov dl, [bx + di]
            cmp dl, 13
            je Done_LN
            _PutCh dl
            inc bx
            jne IsSpace   
    Done_LN: 
               _PutCh 44
               ret
    IsolateLastName ENDP

    PrintFirstMiddle PROC
        xor bx, bx
        mov di, offset Buffer
FirstLast:
        mov dl, [bx + di]
        cmp dl, '$'
        je DoneFM
        _PutCh dl
        inc bx
        jne FirstLast

DoneFM:
        ret
PrintFirstMiddle ENDP
    END Main
        END Main

The function that I am having problems with is PrintFirstMiddle. The way this program works,(supposed to work) start from the back and go backwards till you see a space, this then means the last name has already gone past. Then set that space to a $ so I have that as a null terimator. Now I need to go back and print characters until I see that dollar sign that seperates the First and Middle Name with the last name.

OutPut:

Please enter your full name (First Middle Last): Joe Jon Doe
 Pleaase enter your full name (First middle Last):

Then the program quits and doesn't hang like if it were waiting for input.


Solution

  • The only things I can really see in this code is that you intended to put a $ just before the last name. The line:

    mov [di], '$'
    

    Should probably be:

    mov byte ptr [bx + di], '$'
    

    The above would move the $ dollar sign into the byte pointed to by BX + DI . This would have the effect of terminating the first name and middle name. That should mean you only have to do this to print out the first and middle name:

    PrintFirstMiddle PROC
            _PutStr Buffer
            ret
    PrintFirstMiddle ENDP
    

    Since Buffer is the start of the first name and middle name, and it is now terminated by the $ you should be able to just print the middle and last name with _PutStr Buffer