组装(NASM)如何进行数值运算 [英] Assembly (NASM) How to do Numerical operations

查看:89
本文介绍了组装(NASM)如何进行数值运算的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用Java编写了很多程序,并且了解python的基础知识,有时我会玩c ++. 这些都是高级编程语言,它们都很出色,而且所有这些,但是我很好奇这种情况下汇编语言在更深层次上的工作方式.我开始学习x86处理器的汇编,并选择NASM作为我的汇编器. 我花了一些时间来学习寄存器和堆栈的工作方式,信息的存储方式以及信息的更改方式,不仅是阅读,而且是练习,使用调试器等,这些事情真的开始困扰我: 数值运算...

I programmed a lot in java and know the basics of python and sometimes I play with c++... And those are all High Level Programming Languages and they're great and all but I was curious about how things worked at a deeper level in this case assembly language... I started to learn assembly for the x86 processor and chose NASM as my assembler. I spent some time learning how registers and the stack work, how the information is stored and how the information can be changed , not just reading but practicing, using a debugger, etc and something really started to bother me: NUMERICAL OPERATIONS...

我的意思是,每当从输入中读取某些内容时,它都会作为对应的ASCII值存储在内存中,我可以接受...我了解整个过程中如何读写控制台的基础知识偏移量和长度系统,但是正如我之前提到的那样,包括数字在内的字符都存储为ASCII值..0存储为0x30,1存储为0x31,依此类推...这意味着单个数字存储为整个字节,我看到这是一个缺点,因为我知道一个字节最多可以表示255个数字,这将节省大量内存和时间来进行数学运算,并为其他数据留出空间...在c ++等语言中,整数值仅由4个字节表示,这意味着这些语言最多可以存储4294967295的整数

What I mean is whenever something is read from input it is stored as the correspondent ASCII value on the memory and I'm ok with it... I understand the basics of how to read and write to console with the whole pass offset and length system however as I mentioned before the characters including numbers are stored as ASCII values.. 0 is stored as 0x30, 1 as 0x31, etc... That means a single digit is stored as a whole byte and I see that as a disadvantage because I know a byte can represent numbers up to 255 which would save a lot of memory and time to do math operations and leave space for other data... In languages like c++ an integer value is represented by only 4 bytes which means those languages can store integers up to 4294967295

说到重点... 我真正想知道的是代表数字并使用nasm执行基本算术运算的最佳方法是什么.我应该将这些数字保留为ASCII值并在字节级别上进行运算,还是应该转换这些数字以使字节可以持有更大的价值?还是有一种更有效地读取数字的方法?

Getting to the point... What I really want to know is what is the best approach to represent numbers and do basic arithmetic operations using nasm.. should I leave those numbers as ASCII values and do the operations on a byte level or should I convert those numbers so a byte can hold a larger value ? Or is there a way to read numbers more efficiently ?

PS:很抱歉,我只想提供我所关注的总体背景,以便你们可以以此为基础为我提供帮助...而且对于英语不好的人,我也想尽可能地清楚一点可以(英语不是我的母语,但我会尽我所能...编程时对英语有很多了解)

PS.: Sorry for the long post, I just wanted to give an overall background of my concern so you guys can help me based on it... And for the bad english ... I tried to be as clear as I can (English is not my native language but I try to understand it as much as I can... It helps a lot knowing english when I'm programming)

推荐答案

首先,您不能...我重复一遍,您无法将Assembly与任何高级语言进行比较!它们如何存储数据以及如何与数据交互是完全不同的.他们以自己的方式使程序员的生活更轻松.如果要将char的ASCII码存储为DWORD,请继续.

First off, you cannot... I repeat, you cannot compare Assembly to any High Level Language! How they store and interact with data is totally different. They do it their way to make the programmers life easier. If you want to store the ASCII code of a char as a DWORD, go ahead.

在组装中,绝对没有人手. CPU不知道您要如何处理任何数据,无论是文件中的字节还是键盘上输入的字符.

In Assembly, there is absolutely no hand holding. The CPU has no idea what you want to do with any data, be it a byte in a file or a character entered on the keyboard.

您可以在组装中自行完成所有操作.您在键盘上按下的字符是ASCII,因为当您键入它们时会显示该字符,如果您想对它们进行其他操作,则程序员可以根据需要进行转换.

You do everything yourself in Assembly. The characters you press on a keyboard are ASCII because that is what gets displayed when you type them, if you want to do something else with them, you the programmer converts it however you need.

将ASCII数字转换为数字很简单.

Converting to an ASCII number to a number is trivial.

Numbers: 0  1  2  3  4  5  6  7  8  9
ASCII:   48 49 50 51 52 53 54 55 56 57

看到它们之间的区别了吗? 48是幻数.

See the difference between them? 48 is the magic number.

您遍历字符串,测试每个数字以查看是否介于48到57之间,其他任何内容都将跳过.如果ASCII字符在48到57之间,请从字符中减去48,然后得到一个数字.您添加48以获取数字的ASCII字符.

You loop through your string, testing each digit to see if is between 48 and 57, anything else skip. If the ASCII char is between 48 and 57, subtract 48 from the character and you have a number. You add 48 to get the ASCII char of the number.

不告诉您使用atodw/atoi或其他任何东西,您可以自己动手学习更多,而且并不难.

Not telling you to use a atodw/atoi or whatever, you learn more by doing yourself, and it isn't hard.

这篇关于组装(NASM)如何进行数值运算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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