Lupu Andy
Lupu Andy

Reputation: 21

Enter and Display a vector in x86 Assembly

I am trying to learn TASM Assembly and i need your help. I made this code which introduces a vector from the keyboard and than it displays it on the screen with the elements sum. The problem is that when it displays it displays some weird characters but the sum works. Hope you can help me

TITLE vectors
.model small
.stack 100H 
.data
        msg1   db 10,13,"Enter the lenght of the vector$"   
        msg2   db 10,13,"Enter the vector elements $"    
        msg4   db 10,13,"The sum is $"
        msg3   db 10,13,"The entered vector is $"
        msg5   db "  $"
        vector db 0
        sum    db 0
        x      db 0
.code

main PROC
    MOV ax,@data    
    MOV ds,ax
    MOV ah,9h
    LEA dx, msg1   
    int 21h               
    MOV ah,1h             
    int 21h    
    LEA Si,vector
    MOV cl , al 
    MOV x,al
    SUB cl , 30h  
    MOV sum , 0 
  Introducere:
    MOV ah, 9h
    LEA dx, msg2  
    int 21h
    MOV ah,1h 
    int 21h
    SUB al,30h     
    MOV [Si] , al  
    ADD Si, 1       
    ADD suma,al            
    DEC cl           
    JNZ Introducere  
    JZ Afisare1             
  Afisare1:
    MOV ah,9h
    LEA dx, msg3
    int 21h   
    MOV cl,x
    SUB cl,30h  
    LEA Si,vector  
    JMP Afisare2
  Afisare2:
    MOV dx,[Si]
    ADD dx,30h
    MOV ah,2h
    int 21h
    LEA dx,msg5  
    int 21h
    INC Si
    DEC cl
    JNZ Afisare2
    JZ Afisare3
  Afisare3:
    MOV ah,9h
    LEA dx,msg4  
    int 21h
    MOV dl,sum
    ADD dl,'0' 
    MOV ah,2h   
    int 21h
    MOV ah,04ch  
    int 21h
main ENDP
END main

Upvotes: 1

Views: 3182

Answers (1)

Sep Roland
Sep Roland

Reputation: 39556

The problem is that when it displays it displays some weird characters but the sum works.

These weird characters come from the ommission of the required function number for displaying your msg5. Currently, instead of showing a nice separating space, you get output of the low byte from msg5's address.

MOV ah,2h
int 21h
LEA dx,msg5
                <<<<< Here is missing `mov ah, 09h`
int 21h
vector db 0
sum    db 0
x      db 0

With this definition of vector you reserve just 1 byte to store your inputs. That's not enough! Since your entire program works with single digit numbers, the length of the vector could range from 1 to 9. Therefore you need to make this change:

vector db 9 dup (0)   ;This reserves 9 bytes
sum    db 0
x      db 0

Since your entire program works with single digit numbers and that you output the sum as a single digit number also, you can not be too generous with the values for the inputted numbers. An example that will work follows:

Enter the lenght of the vector3       <<<<< You're missing a space character here!
Enter the vector elements 2
Enter the vector elements 5
Enter the vector elements 1
The entered vector is 2 5 1 
The sum is 8

Upvotes: 1

Related Questions