Converting an ascii character into decimal in Assembly for use with WriteConsoleA and readConsoleA

若如初见. 提交于 2020-01-14 03:37:14

问题


I am in a fix. I need to input numbers from the user and then perform arithmetic calculations on the input. I'm just a beginner now and till now I've not used any IO, except in a program which asks the user for a name and prints an output, using WriteConsoleA and ReadConsoleA. I couldn't find any help on google, on how to convert ascii characters to decimals for input and how to convert decimals to ascii for output. I'd prefer to do this manually before using any of the library functions. I can't figure how to convert a single character into a string and vice versa. How is this done? And yeah, if you write code in the answers, if, possible, use masm syntax as i am not familiar with the other assemblers' syntax. Thanks!! Devjeet


回答1:


If you have a decimal like 31h on al, you can just add 30h to each one to convert them to ASCII characters, like (AL = 31h):

mov cl,al
shr al, 4 // now al = 03
add al,30h // now al = 33h, which if you output is the ASCII character '3'

Now, you can restore al and shift left by 4 to get the next '1' and again add 30h. I hope this illustrates the point :)



来源:https://stackoverflow.com/questions/5785692/converting-an-ascii-character-into-decimal-in-assembly-for-use-with-writeconsole

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!