Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/assembly/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Assembly 汇编语言程序_Assembly_Lc3 - Fatal编程技术网

Assembly 汇编语言程序

Assembly 汇编语言程序,assembly,lc3,Assembly,Lc3,如何将用户输入的任何字符转换为相应的十进制值?我刚开始有点困难 该计划必须实现以下目标: 该程序接受来自键盘的字符 如果字符是数字(“0”到“9”): a) 将字符转换为其对应的十进制值。换句话说,“0”变为零,“1” 变成1,…'9'变成9。让我们将该值称为R(表示“运行长度”)。 b) 等待另一个字符(使用GETC)。 c) 将该字符的R个副本打印到控制台。) d) 返回到步骤1 否则,如果字符为Enter/Return(ASCII#10):将换行符(ASCII#10)打印到控制台,然后 返

如何将用户输入的任何字符转换为相应的十进制值?我刚开始有点困难

该计划必须实现以下目标:

  • 该程序接受来自键盘的字符

  • 如果字符是数字(“0”到“9”): a) 将字符转换为其对应的十进制值。换句话说,“0”变为零,“1” 变成1,…'9'变成9。让我们将该值称为R(表示“运行长度”)。 b) 等待另一个字符(使用GETC)。 c) 将该字符的R个副本打印到控制台。) d) 返回到步骤1

  • 否则,如果字符为Enter/Return(ASCII#10):将换行符(ASCII#10)打印到控制台,然后 返回到步骤1

  • 否则,如果字符是其他字符,则停止程序
    正如所提出的,问题有点不清楚:输入已经是一个十进制值

    如果你的真正意思是如何将输入转换成一个可以进行数学运算的二进制表示,那么基本上你从每个字符值中减去48(30h),将每个数字转换成一个数值


    如果有多个输入数字,则循环它们,并在每次迭代(最后一个除外)中将累加器乘以10。

    显然,您在这里问了同样的问题:

    如果你发布你的代码,将来会有所帮助

    我没有按照转换的需要处理这种类型的问题,相反,我做了一些检查,看看数字是什么,并打印了一个等价的字符串。希望我的节目能给你一些想法:

    .ORIG x3000 ; start at x3000 above system memory
    LEA R0, CLASS   ; load address of string CLASS in R0
    PUTS        ; use PUTS to output string to console
    LEA R0, MYNAME  ; repeat steps for class template
    PUTS
    LEA R0, PRNUM
    PUTS
    LEA R0, NWLINE
    PUTS
    
    ; now get a character from the user and echo it back
    ; using the prompts INPROM and OUTPROM
    ASKINP  LEA R0, INPPROM
        PUTS
        GETC        ; get a character from the keyboard
        ADD R2,R0,#0    ; move the value in R0 to R2 for later comparisons
    
    ; PROCESS THE INPUT
    ; first figure out if they entered a non-printing character with ASCII code
    ; <32. If so, suppress the printing of the character and give the user the
    ; standard error that they have not entered a number. We do this by subtracting
    ; 33 from the input and if the CC is negative, it's a non-printing character. 
    ; For purposes of this program a space is considered a non-printing character.
        ADD R2, R2, #-15
        ADD R2, R2, #-15
        ADD R2, R2, #-3 ; subtracted 33, see if it's negative
        BRn XERR    ; if so branch to error message
    
    ; if we're still here, then it's a printable character and we continue here...
        OUT     ; echo the character back on the input line
        LEA R0, SPACE   ; need a space after the input
        PUTS
        AND R0, R0, #0  ; set the condition to zero again
    
    ; figure out what character the user entered, and we have already subtracted 33 from
    ; the entry. See if it's a ! and the user wants to end. If so, the CC
    ; will be Zero and we break to XDONE.
    ; if that's not it, subtract another 15 (for a total subtracted of 48) and 
    ; successively test to see if it's a number match. If there is no match, 
    ; then print out that the user has not made a correct entry
    
    ; now we subtracted 33 so if they entered ! we now have 0 in R2, subtract 0
    ; and test the CC to see if it's Zero.
        ADD R1, R2, #0
        BRz XDONE
    ; if we have not just branched, subtract another 15 and keep testing
        ADD R2, R2, #-15
        ADD R1, R2, #-9
        BRz XNINE
        ADD R1, R2, #-8
        BRz XEIGHT
        ADD R1, R2, #-7
        BRz XSEVEN
        ADD R1, R2, #-6
        BRz XSIX
        ADD R1, R2, #-5
        BRz XFIVE
        ADD R1, R2, #-4
        BRz XFOUR
        ADD R1, R2, #-3
        BRz XTHREE
        ADD R1, R2, #-2
        BRz XTWO
        ADD R1, R2, #-1
        BRz XONE
        ADD R1, R2, #0
        BRz XZERO
    ; not a quit signal or number, need to print out an error and start again
        BRnzp XERR
    
    ; here is the section that prints out the string related to the 
    ; number that was entered, by loading the string into R0 and
    ; using PUTS to print it out, then BReak back to the beginning
    ; of the input sequence and ask again. Pre-condition is that the
    ; user typed in a number for 0-9, if not they will get an error
    ; elsewhere in the program
    XZERO   LEA R0, ZERO
        PUTS
        LEA R0, NWLINE
        PUTS
        BRnzp   ASKINP  
    XONE    LEA R0, ONE
        PUTS
        LEA R0, NWLINE
        PUTS
        BRnzp   ASKINP
    XTWO    LEA R0, TWO
        PUTS
        LEA R0, NWLINE
        PUTS
        BRnzp   ASKINP
    XTHREE  LEA R0, THREE
        PUTS
        LEA R0, NWLINE
        PUTS
        BRnzp   ASKINP
    XFOUR   LEA R0, FOUR
        PUTS
        LEA R0, NWLINE
        PUTS
        BRnzp   ASKINP
    XFIVE   LEA R0, FIVE
        PUTS
        LEA R0, NWLINE
        PUTS
        BRnzp   ASKINP
    XSIX    LEA R0, SIX
        PUTS
        LEA R0, NWLINE
        PUTS
        BRnzp   ASKINP
    XSEVEN  LEA R0, SEVEN
        PUTS
        LEA R0, NWLINE
        PUTS    
        BRnzp   ASKINP
    XEIGHT  LEA R0, EIGHT
        PUTS
        LEA R0, NWLINE
        PUTS
        BRnzp   ASKINP
    XNINE   LEA R0, NINE
        PUTS
        LEA R0, NWLINE
        PUTS
        BRnzp   ASKINP
    XERR    LEA R0, INPERR
        PUTS
        LEA R0, NWLINE
        PUTS
        BRnzp   ASKINP
    XDONE   LEA R0, DONE
        PUTS
        LEA R0, NWLINE
        PUTS
        HALT
    
    ; store the following strings with these labels
    NWLINE  .STRINGZ    "\n"
    SPACE   .STRINGZ    " "
    CLASS   .STRINGZ    "CS2810\n"  
    MYNAME  .STRINGZ    "James Lohse\n"
    PRNUM   .STRINGZ    "Project 3\n"
    BYEBYE  .STRINGZ    "Program execution terminated!\n"
    INPPROM .STRINGZ    "Input a number 0-9: "
    INPERR  .STRINGZ    "Error! You did not input a number."
    DONE    .STRINGZ    "Done!"
    ZERO    .STRINGZ    "zero"
    ONE .STRINGZ    "one"
    TWO .STRINGZ    "two"
    THREE   .STRINGZ    "three"
    FOUR    .STRINGZ    "four"
    FIVE    .STRINGZ    "five"
    SIX .STRINGZ    "six"
    SEVEN   .STRINGZ    "seven"
    EIGHT   .STRINGZ    "eight"
    NINE    .STRINGZ    "nine "
        .END
    
    .ORIG x3000;从系统内存上方的x3000处开始
    LEA R0级;R0中字符串类的加载地址
    放;使用PUTS将字符串输出到控制台
    LEA R0,我的名字;对类模板重复步骤
    放
    lear0,PRNUM
    放
    LEA R0,NWLINE
    放
    ; 现在从用户处获取一个字符并将其回显
    ; 使用INPROM和OUTPROM提示
    ASKINP LEA R0,INPPROM
    放
    GETC;从键盘获取字符
    加上R2,R0,#0;将R0中的值移动到R2,以便以后比较
    ; 处理输入
    ; 首先确定他们是否输入了ASCII码的非打印字符
    
    ; 我已编辑了原始问题。让我知道现在是否更清楚,我相信我可以用一个检查值而不是原始语句的循环更优雅地完成这项工作。你看一下这个吗?是的,每个单个数字的比较和分支顺序都是疯狂的><如果你想要英文文本名称,就制作一个指向你索引的字符串的指针表。(或者将字符串填充到固定长度,然后计算偏移量以获取以0结尾的字符串的地址。)