This post was updated on .
Hi guys,
I have a question about printing binary data on the hack computer screen. Let's say a int type data: 0000000011111111 (decimal 255) is stored in an address of the hack computer memory. When i use Output.printInt() to show the data on the screen, it is totally fine 255 is printed. But my question is how this is implemented? How Output.printInt() convert binary integer stored in the memory to correct ascii representation (decimal) of the integer? I have completed Output,String,Array class, but i still don't get how integer stored in the memory can be printed on the screen. Could anyone please share/explain your understanding with me? Thanks a lot peter |
The conversion is implemented in String.setInt and is described quite well in the book. The only thing you need to figure out is how to convert a single digit number to its corresponding character.
Hint: characters in Jack are also represented as numbers. |
This post was updated on .
Hi ivant,
Thanks for replying my question. Yes, I see the String.setInt() convert decimal to string, then print on the screen using stored ascii array bit map. But my question is how binary data stored(binary representation of integer) in the memory is converted to decimal data (ascii representation of the integer). Same example here: 0000000011111111 (decimal 255) -> 110010(0x32/50) 110101(0x35/53) 110101(0x35/53) [Ascii representation of decimal 255 in binary]. So when the hack computer check the memory location which contains 0000000011111111 (decimal 255), i think some conversion are needed to be done in order to find [Ascii representation of decimal 255 in binary], then use the stored bit map to print the corresponding shape of the number on the screen. After some search online, I realize if there is an additional hardware/software required to convert binary to bcd(binary coded decimal), then use bcd representation to find corresponding bit map of each integer and showing on the screen. Thanks Peter |
There is a big difference between decimal data and ASCII representation of decimal data. Don't confuse them. The binary number 0000000011111111 is stored as 8 "zeroes" followed by 8 "ones" (they are in quotes, because the hardware uses voltages for this, but for us they are bits). The decimal number 255 is stored in the computer as 0000000011111111. That's because 255 is just a representation for us humans. If a program needs to print the number 0000000011111111 in decimal it has to do so decimal digit by decimal digit. You can get to the last decimal digit by dividing the number to 1010 (10 in decimal) and getting the remainder. This remainder is a decimal digit, that is a number between 0 and 9 inclusive. The ASCII codes for the characters from '0' to '9' are helpfully put one after the other, so all you need to do to get the ASCII code is to add some fixed number to the one you already have. This number is of course the ASCII code of the character '0', or 48 in decimal. OK, so we got the character representation of the last decimal digit. But how can we get the others? Well, if you use integer division and divide 0000000011111111 by 1010 (the same numbers as before), you'll end up with 0000000000011001 which is just 25 in decimal. So in effect using the integer division and remainder, you can split the last digit from the number. In decimal this will look like this: 255 % 10 = 5 // This is the last digit as a decimal number. The value in binary will be 101. 255 / 10 = 25Now you can use the same trick to "peal off" the last digit of 25. You stop when the number you need to represent is a single digit one. Note that this works for non-negative numbers. For negative ones you first negate them thus getting their opposite positive number and add the minus sign to the front of the ASCII representation. This algorithm is also described in the book, in the String.setInt section. BCD is a system used mainly in mainframes by IBM, DEC and others. It's not used by modern computers, although the x86 architecture supports it. |
Hi ivant,
Thank you so much for detailed explanation. Yes you are right, i was confused with Ascii representation of the numbers. I was on the wrong impression of the String.setInt() function, i thought it was converting decimal integer to the Ascii representation of the decimal number. After some careful rethinking, i realised the conversion is done in the form of the binary, so the binary integer is converted to separate Ascii representation of the decimal number through the String.setInt() function. I got carried away by the abstraction(decimal integer arithmetic operation), everything(division,multiplication) under the hood are implemented in binary arithmetic operation. So after String.setInt(), binary integer already converted into Ascii representation of the decimal ineger. Really appreciate your detailed explanation and time. Thanks again peter |
Free forum by Nabble | Edit this page |