【问题标题】:Colors 0-15 Assembly颜色 0-15 装配
【发布时间】:2015-04-01 21:19:35
【问题描述】:

晚安,

我正在尝试使用程序集(FASM)做一个项目,我需要做一些三角形并将 2 种颜色从 0 到 15(向使用程序的人询问数字)

我得到这个来“读取”这些值:

    mov ah, 40h
    mov bx, 1
    mov cx, 22
    mov dx, color1msg
    int 21h

    mov ah, 3Fh
    mov bx, 0
    mov cx, 1
    mov dx, color1
    int 21h

    mov ah, 3Fh
   mov bx, 0
   mov cx, 2
   mov dx, crlf
   int 21h

   mov ah, 40h
   mov bx, 1
   mov cx, 1
   mov dx, paragrafo
   int 21h

   mov ah, 40h
   mov bx, 1
   mov cx, 22
   mov dx, color2msg
   int 21h

   mov ah, 3Fh
   mov bx, 0
   mov cx, 1
   mov dx, color2
   int 21h

   mov ah, 3Fh
   mov bx, 0
   mov cx, 2
   mov dx, crlf
   int 21h

   mov ah, 40h
   mov bx, 1
   mov cx, 1
   mov dx, paragrafo
   int 21h  

   sub [color1], 48
   sub [color2], 48  


   color1msg db "Defina a cor 1 (0-9): "   ;insert the color one 0-9
   color2msg db "Defina a cor 2 (0-9): "     
   paragrafo db 10 
   crlf rb 2 
   color1 rb 2
   color2 rb 2    

但这只能让我从 0 读到 9,谁能帮我把这个从 0 读到 15 吗?

【问题讨论】:

  • 为什么不使用 fn 0x0a 进行缓冲输入?
  • 我并不担心缓冲输入,我只需要有人帮助我读取从 0 到 15 的颜色
  • 从哪里读取?从文件?我以为你希望用户输入数字 0 到 15
  • 是的,但是由于 ASCII 表的原因,您无法读取高于 10 的数字,您需要将它们拆分并相乘,但我不知道该怎么做..
  • 使用 DOS 函数 0x0A 将用户输入读取到缓冲区。指定长度为 2 的缓冲区。然后将带数字的 ASCII 字符串转换为数字

标签: assembly fasm


【解决方案1】:

您可以要求用户输入十六进制数字 A-F 来表示颜色 10-15。这对您的程序的更改最少。
改变这个

sub [color1], 48
sub [color2], 48  

进入

mov al,[color1]
cmp al,65
jbe tt1   ;0-9
sub al,7  ;A-F
tt1:
sub al,48
mov [color1],al

mov al,[color2]
cmp al,65
jbe tt2   ;0-9
sub al,7  ;A-F
tt2:
sub al,48
mov [color2],al

还可以通过更改提示让用户知道。

color1msg db "Defina a cor 1 (0-9 A-F): "
color2msg db "Defina a cor 2 (0-9 A-F): "

【讨论】:

  • 这很清楚,谢谢 但是,我需要输入数字 0-15,这是大学的项目 :(
【解决方案2】:
mov ah, 3Fh
mov bx, 0
mov cx, 2
mov dx, crlf
int 21h

为了能够响应 2 位输入,您需要进行测试以确定之前的代码是否确实获得了字节 13 和 10。我建议如下

 ...
 mov ah, 3Fh
 mov bx, 0
 mov cx, 1
 mov dx, color1
 int 21h
 mov ah, 3Fh
 mov bx, 0
 mov cx, 2
 mov dx, crlf
 int 21h
 ; - - - - - - - - - -
 sub [color1],48
 mov ax, [crlf]
 cmp ax, 0A0Dh
 je OneDigit
TwoDigits:
 sub al, 48
 mov ah, [color1]
 aad             ; AL=AH*10+AL
 mov [color1], al
 mov ah, 3Fh     ; Fetch the still pending linefeed from DOS
 mov bx, 0
 mov cx, 1
 mov dx, crlf
 int 21h
OneDigit:

编辑

完成的程序如下。这应该解决 cmets 中表达的问题。

 mov ah, 40h
 mov bx, 1
 mov cx, 23
 mov dx, color1msg
 int 21h

 mov ah, 3Fh
 mov bx, 0
 mov cx, 1
 mov dx, color1
 int 21h

 mov ah, 3Fh
 mov bx, 0
 mov cx, 2
 mov dx, crlf
 int 21h

 sub BYTE [color1], 48
 mov al, BYTE [crlf]
 cmp al, 13
 je OneDigit_1
TwoDigits_1:
 sub al, 48
 mov ah, [color1]
 aad             ; AL=AH*10+AL
 mov [color1], al
 mov ah, 3Fh     ; Fetch the still pending linefeed from DOS
 mov bx, 0
 mov cx, 1
 mov dx, crlf
 int 21h
OneDigit_1:

 mov ah, 40h
 mov bx, 1
 mov cx, 1
 mov dx, paragrafo
 int 21h



 mov ah, 40h
 mov bx, 1
 mov cx, 23
 mov dx, color2msg
 int 21h

 mov ah, 3Fh
 mov bx, 0
 mov cx, 1
 mov dx, color2
 int 21h

 mov ah, 3Fh
 mov bx, 0
 mov cx, 2
 mov dx, crlf
 int 21h

 sub BYTE [color2], 48
 mov al, BYTE [crlf]
 cmp al, 13
 je OneDigit_2
TwoDigits_2:
 sub al, 48
 mov ah, [color2]
 aad             ; AL=AH*10+AL
 mov [color2], al
 mov ah, 3Fh     ; Fetch the still pending linefeed from DOS
 mov bx, 0
 mov cx, 1
 mov dx, crlf
 int 21h
OneDigit_2:

 mov ah, 40h
 mov bx, 1
 mov cx, 1
 mov dx, paragrafo
 int 21h  

 ...

 color1msg db "Defina a cor 1 (0-15): "
 color2msg db "Defina a cor 2 (0-15): "
 paragrafo db 10
 crlf rb 2
 color1 rb 1
 color2 rb 1

【讨论】:

  • 这不能正常工作,它读取 1 或 2 位数字是的,但是颜色是错误的,如果你输入 15 它显示灰色而不是白色颜色表:0 - 黑色 4 - 红色 8 -深灰色 12 - 浅红色 1 - 蓝色 5 - 品红色 9 - 浅蓝色 13 - 浅品红色 2 - 绿色 6 - 棕色 10 - 浅绿色 14 - 黄色 3 - 青色 7 - 浅灰色 11 - 浅蓝色 15 - 白色
  • 通过使用您的代码(复制粘贴它)我无法运行它这一行 mov ax,[crlf] 给我的操作数大小不匹配,如果我将 ax 更改为 al 或 ah它确实可以运行,但存在 Vasco Lopes 描述的问题。仍然无法使其正常工作我还尝试将 cmp 更改为 ah 或 al 我得到的值超出范围
  • @PedroCavaleiro 我在答案中添加了一个完整的程序。它适用于我的 DOS,因此它应该解决您的 cmets 中表达的问题。
猜你喜欢
  • 2015-06-09
  • 1970-01-01
  • 2022-06-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-11
  • 2023-01-25
  • 1970-01-01
相关资源
最近更新 更多