想要将变量的值保存到寄存器中

时间:2016-12-13 17:28:06

标签: assembly masm x86-16 dos dosbox

我正在使用MASM compilor和DOSBOX。我想要将变量中的值保存到寄存器中。我想将num1值保存到cx寄存器中。我怎么能这样做?

      .MODEL SMALL
.STACK  50H
.DATA
num1 db '5'
    NL  DB  0DH, 0AH, '$'        
    msg     db ?,0AH,0DH,"Enter an odd number between 0 to 10:$"
     nxtline db 0Ah,0DH,"$"
.CODE
MAIN PROC
    MOV AX, @DATA
    MOV DS, AX



              LEA DX,msg     
              mov ah,9
              int 21H
              LEA DX,nxtline    
              mov ah,9
              int 21H

              MOV AH,1                    
              INT 21H 
              LEA DX,nxtline    
              mov ah,9
              int 21H



       mov bl,al   ;save the value from input
              mov num1,bl
               LEA DX,num1     
              mov ah,9
              int 21H
              mov cl,al 

   main endp
   end main

2 个答案:

答案 0 :(得分:3)

您正在丢失用户在AL中输入的值。你输入一个字符:

          MOV AH,1                    
          INT 21H 

char存储在AL中,但在BL中保存值之前,您会显示换行符:

          LEA DX,nxtline    
          mov ah,9
          int 21H

AL中的值消失了,因为此中断使用AL来显示字符串。解决方案是在{/ 1}} 之前保存值,以显示换行符:

BL

修改:将值移至 MOV AH,1 INT 21H mov bl,al ;save the value from input LEA DX,nxtline mov ah,9 int 21H

CX

答案 1 :(得分:1)

顺便说一句:您发布的来源,无法编译(num1未定义)。

通常,要将值从内存加载到寄存器,您可以使用:

mov reg8|16|32|64,[<memory_address>]

示例:

num1: db 7

汇编程序会将此编译为包含值7的单字节,并且它将记录符号表,标记num1存在,指向该字节。

num2: dw 0x0809

这将被编译为两个字节:09 08(数字的最低有效部分首先进入内存,因此09位于num2地址,08位于{ {1}}地址)。 num2+1标签放入符号表中,指向定义的单词的第一个字节(值num2)。

09

要将mov bl,[num1] ; loads value 7 into bl mov cx,[num2] ; loads value 0x0809 (8*256+9 = 2057 in decimal) into cx mov al,[num2] ; loads value 9 into al mov ah,[num2+1] ; loads value 8 into ah = now ax contains 2057. (ax = ah:al) mov dx,[num1] ; will load dl with 7, and dh with something what is after it ; if you wanted "num1", then this is bug, as "num1" is only 1 byte "wide" ; and dx is 16 bit register, so it's two bytes wide. 的8位值加载到16位寄存器bl,您有几个选项,但都遵循相同的原则,您必须将8位值扩展为16位“宽”

cx

要验证这些是否有效,在调试器中启动代码,并在逐步执行每条指令后观察要更改的寄存器值。

您更新的问题是“如何在x86汇编程序中显示数字”。

请参阅https://stackoverflow.com/tags/x86/info,搜索“如何处理多位数字?”

但首先你应该搜索什么是ASCII,以及计算机中的“字符串”如何工作,以及它们与寄存器中的数值有何不同。

在大多数平台上(也是DOS)你不能只做mov cl,bl ; set's lower 8 bits of cx to bl, doesn't care about upper 8 bits ; ^ unless done on purpose, this is bug, as "cx" has unexpected value movzx cx,bl ; "zx" = zero-extend, value 0xFE (254) will become 0x00FE (254) ; recommended way for 386+ code (does not exist on 8086-80286 CPUs) movsx cx,bl ; "sx" = sign-extend, so value 0xFE (-2) will become 0xFFFE (-2) ; recommended way for 386+ code (does not exist on 8086-80286 CPUs) xor cx,cx ; cx = 0 mov cl,bl ; same as first example, but ch was set to zero with previous ins. ; recommended way for 8086 code mov ch,bl ; ch = bl, cl undefined sar cx,8 ; cx = sign extended bl to 16 bits (bl=0xFE will set cx=0xFFFE) ; "recommended" way for 8086 code (not really, but who cares about 8086 anyway) ; for al -> ax sign extension there's specialized CBW instruction mov cl,bl ; cl = bl, ch undefined and cx,0x00FF ; AND with 0x00FF will clear ch, and keep cl ; not recommended (slower), just example that any valid math works of course 并在屏幕上用单个指令打印出来作为字符串“1234”。您必须首先在某个内存缓冲区中构建一个包含五个字符mov cx,1234的ASCII字符串(来自1234$中的16b数值),然后您可以使用cx来显示该字符串。