Author Topic: Adding two numbers - user input  (Read 27936 times)

Offline shellc0de

  • Jr. Member
  • *
  • Posts: 12
Adding two numbers - user input
« on: June 04, 2013, 02:47:16 PM »
LIke title said i have problem adding two numbers :P
Code: [Select]
section .text
org 0x100

num1 dw 0
num2 dw 0

main:
mov ah,0x1
int 0x21
mov word[num1],ax

mov ah,0x1
int 0x21
mov word[num2],ax
mov ax,[num1]
mov bx,[num2]

add ax,bx
add ax,48
mov ah,0x2
mov dx,ax
int 0x21

exit:
mov ah,0x8
int 0x21
mov ax,0x4c00
int 0x21
I get some random chars.
« Last Edit: June 04, 2013, 02:50:11 PM by shellc0de »

Offline TightCoderEx

  • Full Member
  • **
  • Posts: 103
Re: Adding two numbers - user input
« Reply #1 on: June 04, 2013, 05:30:56 PM »
Let's say you're adding 9 and 3.  num1 becomes 39H or 57 and num2 becomes 33H or 51.  57 + 51 = 108 + 48 = 156. So what you're actually printing is character code 156 and depending upon character set it could be any sort of character.

You need strip MSB from AX with and ax,15.  The next thing you need consider is any computed values over 10 need two digits displayed as in the previous example 12 + 48 = 60 and that is "<".

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: Adding two numbers - user input
« Reply #2 on: June 04, 2013, 05:58:45 PM »
Your character(s ?) is not truely "random", but it won't be the number you're looking for.

The first issue is that the entrypoint for a .com file is the beginning of your .text section. This means that the CPU will attempt to execute your data. The "old school" way is to put "jmp main" first, then your data, then start your code at "main".

Diversion: I really don't like "main" as a label, unless it's a "C-style" main. The word "main" has no special meaning in asm, unlike C. It's as good as any other label name. However, it could confuse a human into thinking you've got a "C-style" main. It does NOT indicate an entrypoint unless you're linking with C. That's just a personal preference... I'll leave it...

In a .com file, everything is in one segment (section). What Nasm does when it sees "section .data" (in "-f bin" output format) is to move it after your code (and "section .bss", if any, after that). Or, you could just move your data to the end with the same effect.

int 21h/1 returns a single character in al - ah is probably still 1. The characters representing decimal digits are not the same as the numbers represented. So if the user types "1", we see 0x31 (or 49 decimal) in al. So ax is 0x131, and this is what you put in [num1]. Not very useful for adding something to. What you want to do is "sub al, '0'" (can write it as "sub al, 48" or "sub al, 0x30" - same code but I like '0' as it makes the purpose clearer... maybe), and then put that value in [num1]. "mov [num1], al", or if you want to use ax, "mov ah, 0" first. It does no harm to put a value into a buffer that's "too big" - putting a value into a buffer that's "too small" is a serious problem!

Do the same with "[num2]". At this point, you can add 'em together. We only have a useful byte, but using 16-bit registers won't hurt (won't do any good, either). Now, if the sum is still one digit, your remaining code will probably work. int 21h/2 prints only the character in dl, but it won't hurt to have something (probably 0) in dh.

If the sum exceeds one digit, you'll need to provide a "itoa" routine of some sort. Since this is probably "the most frequently asked question", you can probably find an example around somewhere. If not. I'll try to come up with something... but not right now... Good luck!

Best,
Frank


Offline shellc0de

  • Jr. Member
  • *
  • Posts: 12
Re: Adding two numbers - user input
« Reply #3 on: June 04, 2013, 09:17:19 PM »
Let's say you're adding 9 and 3.  num1 becomes 39H or 57 and num2 becomes 33H or 51.  57 + 51 = 108 + 48 = 156. So what you're actually printing is character code 156 and depending upon character set it could be any sort of character.

You need strip MSB from AX with and ax,15.  The next thing you need consider is any computed values over 10 need two digits displayed as in the previous example 12 + 48 = 60 and that is "<".

Thanks for replay. I know the problem of adding 9 and 3 i can't get output 12,yes but if i make some loop.i can't add 2+2
Code: [Select]
mov ax,2
mov bx,2
add ax,bx ;result is in ax and i need just to print it.
The difference is that i just get value from user and store it in variable.
I don't have much time now i will see tomorow,but if u can post code example.That will be good. Btw i used start: label,but in this example i used main: because i do C programming , and that is not big problem for my simple program. Thanks

Offline shellc0de

  • Jr. Member
  • *
  • Posts: 12
Re: Adding two numbers - user input
« Reply #4 on: June 05, 2013, 06:50:04 PM »
Now i have tested both.
1# Solution with 'and ax,15' and
2# Solution with 'sub ax,48'
Works perfect.Thanks.
But if i can trace what happens in register i'll solv it my self but when i load linked nasm code ,and open with OllyDbg or ImmDbg they can't understand interrupts.
Can you suggest me a program (debugger) for that?

Offline TightCoderEx

  • Full Member
  • **
  • Posts: 103
Re: Adding two numbers - user input
« Reply #5 on: June 05, 2013, 10:23:51 PM »
I'm assuming what you mean is that they can't trace through interrupts.  On WinXP, I use DEBUG or SYMDEB.

Offline shellc0de

  • Jr. Member
  • *
  • Posts: 12
Re: Adding two numbers - user input
« Reply #6 on: June 07, 2013, 09:15:33 AM »
I need program to track valuse in register in every line of asm code :) to se waht happens like OllyDbg :D but i can't run 16bit code in Olly ,if i like it to .exe [32 bits] olly don't understand interupts in nasm :D

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: Adding two numbers - user input
« Reply #7 on: June 07, 2013, 08:49:17 PM »
For a dos debugger, I used to like David Lindauer's "GRDB". http://ladsoft.tripod.com/grdb_debugger.html It's "like DEBUG, only brighter". Open source, BTW, if you need to know how a debugger works...

When you come to an interrupt, don't try to single-step through it ('t'), use 'p' instead to "proceed" through it and stop again after the interrupt returns. Single stepping through dos interrupts usually crashes DEBUG (or GRDB), as I recall...

That example is still on my to-do list. First, we need a good "specification". We want to input two numbers from the user, and add them. We probably don't want to be limited to single-digit numbers. Do we want to accept negative numbers? What do we want to do if the victim... I mean "user"... enters a non-digit? My inclination is to quit and return whatever we've got so far - even if it's zero. We could yell at 'em and make 'em do it over... What about overflow? It is arguably "correct" to just let it roll over, but this probably isn't the result the user expects. Decisions, decisions... Once I have a solid "specification" in mind, I'll write the comments first, then fill in the code. That's what it says in the "glossy brochure" anyway. We'll see how much ambition I actually have...

Best,
Frank


Offline Gerhard

  • Jr. Member
  • *
  • Posts: 51
Re: Adding two numbers - user input
« Reply #8 on: June 08, 2013, 09:06:17 AM »
Frank,

That example is still on my to-do list. First, we need a good "specification". We want to input two numbers from the user, and add them. We probably don't want to be limited to single-digit numbers. Do we want to accept negative numbers? What do we want to do if the victim... I mean "user"... enters a non-digit? My inclination is to quit and return whatever we've got so far - even if it's zero. We could yell at 'em and make 'em do it over... What about overflow? It is arguably "correct" to just let it roll over, but this probably isn't the result the user expects. Decisions, decisions... Once I have a solid "specification" in mind, I'll write the comments first, then fill in the code. That's what it says in the "glossy brochure" anyway. We'll see how much ambition I actually have...

that's indeed a lot of work to do, Frank. Good luck.

Gerhard

Offline shellc0de

  • Jr. Member
  • *
  • Posts: 12
Re: Adding two numbers - user input
« Reply #9 on: June 08, 2013, 11:31:21 AM »
Thanks a lot. Yea you have right Frank. Thanks Gerhard & Frank :D