如何在汇编语言中将数字作为输入然后将它们打印为数字? [英] How to take digits as input in Assembly Language then print them as a number?

查看:87
本文介绍了如何在汇编语言中将数字作为输入然后将它们打印为数字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是汇编语言的新手,我的老师布置了一个我不知道该怎么做的作业.请帮我理解代码....

I'm new to assembly language and my faculty gave an assignment which I cannot figure out how to do. Please help me understand the code....

赋值如下:将用户的4位数字作为字符串(字母数字到整数),转换成数字并存入AX中

The assignment as follows: Take 4 digits from user as string(alphanumeric to integer), convert them into a number and store in AX

如果用户输入是'1'、'2'、'3'、'4'输出应该是:1234

if user input is '1','2','3','4' the output should be: 1234

附言我不想要代码,我无法理解程序

P.S. I don't want the code, I'm having trouble understanding the procedure

推荐答案

1.征求意见

您在这里的目标是获得一系列字符,这些字符可以作为一个完整的字符串或一个一个地组合在一起.如果取整个字符串,则必须逐个字符地读取它,如果一次取一个字符,则您已经完成了.

1. Ask for input

Your goal here is to get a series of characters, these can be taken together as a whole string or one by one. If you take a whole string then you have to read it char by char, if you take one char at a time you are already done.

您读取的字符是字形、符号.计算机中的符号(与其他一切一样)用数字表示.这种映射的完成方式是您的(字符集)编码.无需进入广阔的编码领域,您就可以依赖这样一个事实,即在大多数西方编码中,十进制数字的字形使用相同的数字进行编码,特别是您可以假设为 ASCII 编码.
如果您查看 ASCII 表,您会看到这个

The chars you read are glyphs, symbols. Symbols in computer are represented (as everything else) with numbers. The way this mapping is done is your (charset) encoding. Without entering the vast realm of encoding you can rely on the fact that the glyphs for the decimal digits are encoded with the same numbers in the majority of western encoding, in particular you can assume ASCII encoding.
If you look at an ASCII table you see this

Character 0    1    2    3    4    5    6    7    8    9
Encoding  30h  31h  32h  33h  34h  35h  36h  37h  38h  39h

因此,如果用户输入 1234,则字符串 31h 32h 33h 34h 中有这些字节.
您的目标是从编码转换为数字,例如字符 '4' 的编码为 34h,您希望将其转换为值 4h.
对于从 2 到 36 的基数,这可以通过基本算术来完成,更花哨的基数可能需要查表.
不要忘记验证输入!检查字符串中的有效字符!

So if the user input 1234 you have these bytes in the string 31h 32h 33h 34h.
Your goal is to convert from the encoding to the digit, for example the char '4' has encoding 34h and you want to transform this into the value 4h.
For base from 2 to 36 this can be done with basic arithmetic, more fancy bases may need look up tables.
Don't forget to validate the input! Check the string for valid characters!

现在你有一个数字序列,我们称它们为 dn, ..., d3, d2, d1, d0 其中 dn 是用户输入的第一个数字和 d0 最后一个.您想根据这些数字创建一个数字.你知道像 1234 这样的数字实际上意味着 1*10^3 + 2*10^2 + 3*10^1 + 4.

Now you have a sequence of digits, let's call them dn, ..., d3, d2, d1, d0 where dn is the first digit the user inputted and d0 the last one. You want to create a number from this digits. You know that a number like 1234 actually means 1*10^3 + 2*10^2 + 3*10^1 + 4.

所以你要做的很简单:
dn*10^n + ... + d3*10^3 + d2*10^2 + d1*10^1 + d0
(这解释了我对下标的选择)

考虑到 n 是变化的(对于 16 位数字,最小值为 1,最大值为 5)您可以通过累加部分结果来简化此计算.
这意味着你有一个部分结果 temp 最初是 0.每次你有一个新的数字 di 你执行
tmp = tmp*10 + di
说服自己,这有效地计算了上面的总和.这种方法免费提供了一个很好的功能,您可以计算数字的低 16 位,这对于不能容纳 WORD 的数字通常是一种优雅的降级.

Considering that n is varying (min is 1 max is 5 for 16 bit numbers) you can simplify this computation by accumulating the partial result.
This means that you have a partial result temp initially 0. Each time you have a new digit di you perform
tmp = tmp*10 + di
Convince yourself that this effectively compute the sum above. This method brings for free the nice feature that you compute the low 16 bit of the number which usually is a graceful degradation for numbers that can't fit a WORD.

n = 0;
t = 0;
while (1)
{
   c = get_char_from_input();
   if (end_of_input(c) || max_digits_read(n))
      break;

   d = get_digit_from_char(c);
   t = t * 10 + d;
   n = n + 1;
}
return t;

开发高效的程序集版本的任务由您决定.

Up to you the task to develop an efficient assembly version.

这篇关于如何在汇编语言中将数字作为输入然后将它们打印为数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆