Printing variable in Assembly (Intel 8088)

sherbetlemon
sherbetlemon used Ask the Experts™
on
Hi all, :)

I'm new to assembly, that is why i'm really having a hard time on it. Anyway I have a programming assignment on my Assembly class . Here's the flow:

1. user input sentence
2. program validates the sentence, must end in period and must not be null,
3. program will count the number of characters, number of words, number of letter 'A', and then print the result.

I have the following code:

          TITLE First Machine Program
          DOSSEG
          .MODEL SMALL
          .STACK 100h
          .DATA
ALPHA      db ?
BETA       db 0
CHARLIE    dw 0
DELTA     DB "A"
FOXTROT   DB 4 dup ("$")
MSG1    	  DB "Enter a sentence Please: $"
MSG2    	  DB "Character count =  $"
MSG3    	  DB "Word count =  $"
MSG4    	  DB "Number of letter A =  $"
MSG5    	  DB "Do you want to try again [y/n]?  $"
CRLF      db 13, 10,"$"
SAGOT    db  84 dup("$")
PR1     db "Hello $"
     .CODE
CLRSCR  PROC
    mov al, 03h
    mov ah, 00h
    int 10h
   RET
CLRSCR ENDP



BEGIN:  mov ax,@data
        mov ds, ax
        mov es, ax
; clrscr
     CALL CLRSCR


       mov al, 03h
       mov ah, 00h
       int 10h

        lea dx, CRLF
        mov ah, 09h
        int 21h

        lea dx, MSG1
        mov ah, 09h
        int 21h
		
		lea dx, CRLF
        mov ah, 09h
        int 21h
		
 ; scanf
      mov byte ptr SAGOT, 81
       lea dx, sagot
       mov ah, 0Ah
       int 21h

        lea dx, CRLF
        mov ah, 09h
        int 21h

        MOV SI, 0002

        lea dx, SAGOT[SI]
        mov ah, 09h
        int 21h

;processing goes here

		mov CHARLIE, 00
	COUNTCHAR: CMP byte ptr SAGOT[SI], '.'
		JE ENDCOUNTCHAR
		INC CHARLIE
		INC SI
	ENDCOUNTCHAR:
		
		mov dh,00
		add charlie,30
		mov DL,AL
		;mov charlie, 30h
		lea dx, CHARLIE
        mov ah, 09h
        int 21h
		
		mov dh,00
		add al,30
		mov DL,AL
		mov ah, 09h
        int 21h
		
	; 2 line breaks before reports
		lea dx, CRLF
        mov ah, 09h
        int 21h
		
		lea dx, CRLF
        mov ah, 09h
        int 21h

;print reports
		lea dx, MSG2
        mov ah, 09h
        int 21h

		lea dx, CRLF
        mov ah, 09h
        int 21h

		lea dx, MSG3
        mov ah, 09h
        int 21h

		lea dx, CRLF
        mov ah, 09h
        int 21h
		
		lea dx, MSG4
        mov ah, 09h
        int 21h

		lea dx, CRLF
        mov ah, 09h
        int 21h
		
		lea dx, CRLF
        mov ah, 09h
        int 21h
		
		lea dx, MSG5
        mov ah, 09h
        int 21h

		lea dx, CRLF
        mov ah, 09h
        int 21h

        mov ah, 4ch
        int 21h
        end BEGIN

Open in new window


I'm trying first to count the number of characters so I have a variable CHARLIE that increments as you traverse the sentence. Problem is, whenever i try to print the value of charlie after the counting of characters, it always prints a symbol, not a number as I expected. Here's the sample result:



Enter a sentence Please:
the quick brown fox jumps over the lazy dog.
¿ A   quick brown fox jumps over the lazy dog.

Character count =
Word count =
Number of letter A =

Do you want to try again [y/n]?

C:\MSIT\FIRSTT~1.201\CCS512\ASM>

The value of CHARLIE, as I print it was:
¿ A   quick brown fox jumps over the lazy dog.

It is supposed to be an integer.
What's wrong with it?? Can anybody please help me?!
thanks in advance.. :D
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
developmentguruPresident

Commented:
It is most likely printing a symbol because you need to convert the number to text before you print it.  I do not have an assembler environment set up at the moment.  If you need more help with this then I would like to know what assembler you are using.  Knowing more about your test environment would help too (OS, etc).

Commented:
obviously you know the function number for int 21 to print text (ah = 9). perhaps you can find also the function to print a number or integer. if not you can build a loop where you divide charlie by 10 (decimal = 0a hex), get the rest and add hex 30 (i assume it prints ascii code) and store the result say at location x. subtract the rest form your division result, jump to the beginning of the loop and store the second result at location x-1. the last looping is when the division result is lower 1. then your text ist stored at x - n, where n is the number of digits of charlie

hope this helps. if you need further assistance i can take a look at my assembler book.

another perhaps easier way is to begin counting at 30 hex and every 10 set your first digit to zero and increment your second digit from beginning also at 30 hex and so on. so you don't need to operate with floats.

Author

Commented:
hi guys!,

I'm using tasm and tlink..

:D
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

HonorGodSoftware Engineer

Commented:
It has been a very long time since I played with 8088 ASM... ;-)

Q: Line 62 - Why is SI set to 2, and not zero?

Q: Does the displaying of the user message (line 66) modify SI?

Q: Did you realize that the only "processing" that you do (starting line 70)
    is to check for a period?

    You don't have a loop of any kind to check all of the user specified data.
    Did you realize that?

    The code that you have (lines 70-75) is equivalent to this Python code:

CHARLIE = 0
if SAGOT[ SI ] != '.' :
  CHARLIE += 1
  SI += 1
...

Open in new window

HonorGodSoftware Engineer

Commented:
Is this the answer to my previous question #1?

Q: Line 62 - Why is SI set to 2, and not zero?
A: Because
    offset 0 = buffer length,
    offset 1 = user specified text length

Author

Commented:
@HonorGod:
That's right I set SI to 2 to point it to the start of the string..
HonorGodSoftware Engineer

Commented:
Did you understand the earlier comment that the "processing" of the user entered data?

You need a loop to check all of the user entered characters.
HonorGodSoftware Engineer

Commented:
Did you see my update?

Do you know how to process the user input?

Author

Commented:
@HonorGod:

Hi! I still have not figured it out. I'm having trouble processing the output..

:(
Software Engineer
Commented:
Let's start by figuring out how to display the length of the user specified data...

How does that sound?

After the buffered user input, you have:

- SAGOT[ 0 ] == Buffer length (i.e., available space)
- SAGOT[ 1 ] == User input length

I would suggest that you try and figure out how to:

- put the decimal value into a register (e.g., AX)
- call a routine to display the decimal value in AX (in the proper order)...

Do you know how to do this?
developmentguruPresident

Commented:
It sounds to me like this is for a class... might want to be careful the code is not beyond what they have learned.
Commented:
to become your length (up to max 999) as printable text i suggest the following:

initialise the folloing:

charlie  db "000$"
digit1   db "0"
digit2   db "0"
digit3   db "0"

then in your counter-loop place:

inc digit1
cmp digit1, 3ah
je incd2
jmp nextinput

incd2:
mov digit1, 30h
inc digit2
cmp digit2, 3ah
je incd3
jmp nextinput

incd3:
mov digit2, 30h
inc digit3
jmp nextinput

after endcountchar: place

mov charlie, digit3
mov charlie + 1, digit2
mov charlie + 2, digit1

lea dx, charlie
mov ah, 09h
int 21h

do you have with the help of honorgod corrected your count-loop?

Author

Commented:
i tried printing the user input length but I still don't get the expected result. Here's what i did to print it:

mov SI, 0001
            lea dx, SAGOT[SI]
            AAM
        mov ah, 09h
        int 21h

i used AAM to convert it to decimal but it still wont work. :'(

Commented:
oh, i ignore you want the length at the beginning of sagot, and the sentence is only 83 bytes long.
so you don't need digit3 and charlie and instead initialise digit1 and digit2 to sagot[0] and sagot[1].

but AAM? i've found the following about this at
http://www.x-ways.net/winhex/kb/resources/Intel_x86_Commands.txt

AAM - Ascii Adjust for Multiplication


        Usage:  AAM
        Modifies flags: PF SF ZF (AF,CF,OF undefined)

        AH := AL / 10
        AL := AL mod 10

        Used after multiplication of two unpacked decimal numbers, this
        instruction adjusts an unpacked decimal number.  The high order
        nibble of each byte must be zeroed before using this instruction.
        This instruction is also known to have an undocumented behavior.

so AAM does nothing with your first 2 bytes of sagem, it expects input in ax and gets results in ax, so i don't understand this at all.
HonorGodSoftware Engineer

Commented:
wk51 is correct.

In fact we can verify this using a program that has been available with Window, and it's predecessor, DOS (Disk Operating System), since DOS 1.0.

The program is called "debug".  If we type "debug" at a command prompt it responds by showing a dash/hyphen ("-") as a prompt.

Typing "r" (for registers), shows us the current contents of the registers.
Typing "rax" allows us to modify the specified register (ax).  A colon (":") prompt is displayed so that we can enter a new value.
Type "49" (which is hexidecimal), and press enter
At the DOS prompt ("-"), type "r" again to verify that the value you specified in now in the AX register.
Now, we can enter an instruction by typing "a" (for assemble) at the DOS prompt.
The current address is displayed to show the location at which the instruction will be entered.
Type "aam" and enter
Another address will be displayed for the next instruction.  Press enter to get back to the DOS command prompt.
Typing "r" again not only shows us the registers, but the "next" instruction to be executed (i.e., CS:IP), which will now be the "AAM" instruction we entered.
Typing "t" will "trace a single instruction" which just happens to be the AAM we entered.
After the "t" is executed, the current registers will be displayed showing us the result of having executed the "AAM" instruction.

Note how the value in AX (0049) has been changed!  It is now 0703!
Is this right?

Let's see.
Start an instance of "Calculator"
Click "View -> Scientific"
Click the "Hex" Radio button (or press the F5 key)
Enter "49" in the input area
Click the "Dec" Radio button (or press the F6 key)
We see that 0x49 == 73, so the result of executing the AAM when AX=0049 correctly results in AH = 07 and AL = 03!

Here's my debug session:

C:\>debug
-r
AX=0000  BX=0000  CX=0000  DX=0000  SP=FFEE  BP=0000  SI=0000  DI=0000
DS=141A  ES=141A  SS=141A  CS=141A  IP=0100   NV UP EI PL NZ NA PO NC
141A:0100 0000          ADD     [BX+SI],AL                         DS:0000=CD
-rax
AX 0000
:49
-r
AX=0049  BX=0000  CX=0000  DX=0000  SP=FFEE  BP=0000  SI=0000  DI=0000
DS=141A  ES=141A  SS=141A  CS=141A  IP=0100   NV UP EI PL NZ NA PO NC
141A:0100 0000          ADD     [BX+SI],AL                         DS:0000=CD
-a
141A:0100 aam
141A:0102
-r
AX=0049  BX=0000  CX=0000  DX=0000  SP=FFEE  BP=0000  SI=0000  DI=0000
DS=141A  ES=141A  SS=141A  CS=141A  IP=0100   NV UP EI PL NZ NA PO NC
141A:0100 D40A          AAM
-t

AX=0703  BX=0000  CX=0000  DX=0000  SP=FFEE  BP=0000  SI=0000  DI=0000
DS=141A  ES=141A  SS=141A  CS=141A  IP=0102   NV UP EI PL NZ NA PE NC
141A:0102 0000          ADD     [BX+SI],AL                         DS:0000=CD
-

Open in new window

HonorGodSoftware Engineer

Commented:
or, I forgot to say that at the Debug prompt (the '-'), type "q" and press enter to quit/exit debug and return to the DOS command prompt.
HonorGodSoftware Engineer

Commented:
This "debug" tutorial may prove useful/helpful

http://www.armory.com/~rstevew/Public/Tutor/Debug/debug-manual.html
HonorGodSoftware Engineer

Commented:
The challenge for this kind of question is that we don't want to, and won't, do all of the work for the asker.

Because of this no complete answer was provided, but this was by choice of the experts, and I believe that it was the right choice.

Suggestion:
- Give wk51 all the points for #a33231478

Commented:
thanks for the suggestion HonorGod, but i think at least half of the points should be yours.

seems sherbetlemon has lost interest in work at solution.
HonorGodSoftware Engineer

Commented:
@wk51 - Thanks.  That's what I really like about Experts-Exchange.  People working together to help others!

Author

Commented:
i still have to review the answers..thanks

Author

Commented:
Users were very helpful.
HonorGodSoftware Engineer

Commented:
Thank you for the grade & points.

Good luck & have a great day.

Hopefully you got all of the help that you needed.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial