Author Topic: Printing Numbers to the Terminal  (Read 7544 times)

Offline 0000.0001

  • Jr. Member
  • *
  • Posts: 2
Printing Numbers to the Terminal
« on: April 19, 2012, 12:09:40 AM »
I am writing an operating system kernel, and am currently working on a calculator program. I have created it so that addition output is correct, but instead of printing numbers, it prints the ASCII character for that number. For example, If I type in 2 + 2, I will get 'd'. How can I make it so that it prints the number instead of the ASCII character?

Offline Rob Neff

  • Forum Moderator
  • Full Member
  • *****
  • Posts: 429
  • Country: us
Re: Printing Numbers to the Terminal
« Reply #1 on: April 19, 2012, 12:57:17 AM »
Search the forums.  There are a huge number of posts with example code regarding this very topic.

Offline 0000.0001

  • Jr. Member
  • *
  • Posts: 2
Re: Printing Numbers to the Terminal
« Reply #2 on: April 19, 2012, 01:44:13 AM »
I searched the forum, and I found a thread that appeared to have the solution to the problem:
Code: [Select]
sub si, '0'
sub di, '0'
Now, instead of printing 'd', it prints 'qwn'. I think that it is because I am not using brackets around si, but I am not sure because it will not assemble correctly if I put brackets around it. Here is some more code, just in case you need it:
Code: [Select]
.calc:
.calcloop:
mov si, math_prompt
call print_string

mov di, buffer
call get_string

mov si, buffer
mov di, math_plus
call strcmp
jc .plus

jmp mainloop

.plus:
mov si, math_prompt_fnum
call print_string

mov di, add_res
call get_string

mov si, math_prompt_num2
call print_string

mov di, add_res2
call get_string

mov si, add_res
mov di, [add_res2]

                                                        sub si, '0'
                                                        sub di, '0'
 
add [si], di

call print_string

mov si, nl
call print_string

jmp mainloop

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: Printing Numbers to the Terminal
« Reply #3 on: April 19, 2012, 03:32:14 AM »
I usually don't put it as bluntly as this, but I'm tired and cranky: You're not ready to write an OS! You probably shouldn't let my grumpy mood discourage you, though...

Here's just a quick hint:
Code: [Select]
sub byte [si], '0'
sub byte [di], '0'
That should assemble, at least, and will convert two characters to (single digit) numbers (assuming that the characters represent decimal digits). Now you can do arithmetic on those numbers. To print the result, you'll need to convert the result (number) to one or more characters. (single-digit numbers aren't very useful, but it's someplace to start, I guess)

As Rob points out, there are many examples - it's a very common problem - but they aren't too well labeled. I'll try to give you more help when I'm in a better mood. Remind me if I forget!

Best,
Frank