【问题标题】:Enter and Display a vector in x86 Assembly在 x86 汇编中输入并显示向量
【发布时间】:2018-11-24 00:12:19
【问题描述】:

我正在努力学习 TASM 组装,我需要您的帮助。 我编写了这段代码,它从键盘引入了一个向量,然后将其与元素总和一起显示在屏幕上。问题是当它显示时会显示一些奇怪的字符,但总和有效。 希望你能帮助我

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

【问题讨论】:

  • 这是一个静态数组。向量要么是 SIMD 向量(如 XMM0 寄存器),要么是 C++ std::vector,它是一个 动态分配可调整大小 容器。但无论如何,您只为db 0 的数组分配了一个字节的空间。可能你用sumx 覆盖了它的下两个字节,但我没有阅读你未注释的双倍行距难以阅读的代码。

标签: arrays assembly sum x86-16 tasm


【解决方案1】:

问题是当它显示时会显示一些奇怪的字符,但总和有效。

这些奇怪的字符来自于省略显示您的 msg5 所需的功能编号。目前,不是显示一个很好的分隔空间,而是从 msg5 的地址中获取低字节的输出。

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

使用 vector 的定义,您只需保留 1 个字节来存储您的输入。这还不够!由于您的整个程序都使用单个数字,因此向量的长度范围可以从 1 到 9。因此您需要进行此更改:

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

由于您的整个程序都使用一位数字并且您也将总和输出为一位数字,因此您不能对输入数字的值过于慷慨。一个可行的例子如下:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-15
    • 1970-01-01
    • 2016-08-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多