Entering and dealing with floating point numbers on IEEE 784 with assembler (NASM 32bit)

给你一囗甜甜゛ 提交于 2019-12-12 00:29:20

问题


I am taking Computer Architecture subject at University, and I was assigned to program a tool which would take floating point number as input, I guess store it in memory and printout hexadecimal form of the binary representation of the number in IEEE 784 standart.

Now I am certain about the algorithm of converting any decimal floating point number to its binary form in IEEE 784 on paper yet I struggle to come up with a solution for assembler (numbers can be such as -157.4, 0.5, -0.6 and etc.).

My guesses are that I would need to extract the sign, the exponent and mantissa from input using ASCII codes and string manipulation and store either 0 or 1 in memory for the sign, convert what's before the . sign to binary form and shift bits right or left till I get one number, storing the amount of times the program had to shift right (that and +127 would be exponent, right?). Then somehow I should deal with the remaining part of the entered numbers (after . ). Should I multiply it by two, like on paper, or is there a method for this sort of problem? Lastly, the program should convert each 4bits to hex, but I am not sure how.

I don't want copy - paste solutions, I am seeking to learn assembler, to understand inner processes, not just finish the assignments. If anyone has ever dealt with such problems, where should I go first. What should I study? I have nearly three weeks for the task.

(last bit - both emu8086 and NASM should be able to assemble the program).

Thank you!


回答1:


You're asking two questions in one.

  1. How to convert a string to an IEEE-754 float? Ok, interesting question, and more complicated than you might expect. Getting even the very last bit rounded correctly requires a lot of work.

  2. How do I do this in x86 asm? Silly question; the same way you code any algorithm in asm. The simplest answer to this is to write it in C, and look at the compiler output. The obsolete x87 FPU has some instructions that do sort of bitwise things with FP values, but normal FPUs like SSE don't have that. SSE just has conversion to/from integers, and normal math ops. (And of course you can use vector-integer ops on the same registers). Anyway, "how do I write this in asm" is not an interesting question if you don't already have a correct algorithm and an attempt at implementing it.


An online IEEE-754 calculator recommends looking at the libc source code for converting strings to floats and vice versa.

I found a nice writeup and analysis of GNU libc's strtof/strtod functions, which also compares it to David Gray's implementation which is used in Python, PHP, Java, and several web browsers, among other things.

quick summary:

  1. Parse the decimal string into integer and fractional parts.

  2. Convert those parts to binary separately, to extended-precision integers (using gmp routines).

  3. If the integer part has enough significant bits to fill the mantissa, you don't have to look at the fractional part. Otherwise, get bits from the fractional part.

The fractional part is tricky: it can have leading zeros, so it's represented as a fraction. e.g. .00123 = 123/105. Getting the required number of bits for the mantissa out of this fraction is done with extended-precision integer division, typically only using a couple div instructions rather than computing and throwing away most of the full extended-precision division result.


Once you have a 32bit float value, you can print it as a hex string the same way as you would for any 32bit integer. Base16 is super convenient, because every 8 bits maps to two hex digits. Printing integers in base10 typically requires repeated division by 10 until the number is <= 9, but in base16 that's just a bit-shift by 4.



来源:https://stackoverflow.com/questions/32836428/entering-and-dealing-with-floating-point-numbers-on-ieee-784-with-assembler-nas

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