Author Topic: Adding user input.  (Read 7350 times)

Offline shadyPengi

  • New Member
  • Posts: 1
Adding user input.
« on: September 25, 2016, 06:16:06 AM »
Hi, i have one question, how to work with 2characters(or more) number? for example 15 5 how to add 15+5 subtract 15-5 multiply 15*5 and div 15/5  to get proper answer from user input? Because how to work with single numbers i figure it out, but when the answer goes to 2digits i get some weird characters( i think they are ASCII characters) here is my code:
Code: [Select]
section .data
msg:db"First: "
msg_L: equ $-msg
br:db"",10
br_L: equ $-br
msg2:db"Second: "
msg2_L: equ $-msg2

section .bss
First resb 255
Second resb 255
Sum resb 255

section .text
global _start

_start:
mov eax,4 ;Propmpts to enter 1st num
mov ebx,1
mov ecx,msg
mov edx,msg_L
int 80h
mov eax,3 ;gets user first number
mov ebx,1
mov ecx,First
mov edx,255
int 80h

mov eax,4 ;break line
mov ebx,1
mov ecx,br
mov edx,br_L
int 80h

mov eax,4 ;propmpts to enter 2nd number
mov ebx,1
mov ecx,msg2
mov edx,msg2_L
int 80h
mov eax,3;gets users second number
mov ebx,1
mov ecx,Second
mov edx,255
int 80h

mov eax,4 ;break
mov ebx,1
mov ecx,br
mov edx,br_L
int 80h




;////////////////////////////////////////////
mov al,[First] ;moving to al register
mov bl,[Second] ;moving to bl register

sub al,'0'
sub bl,'0'

add al,bl
add al,'0'

mov [Sum],al ;seting Suma to al
;///////////////////////////////////////////////


mov eax,4;showing the answer
mov ebx,1
mov ecx,Sum
mov edx,255
int 80h

mov eax,4
mov ebx,1
mov ecx,br
mov edx,br_L
int 80h

mov eax,1
mov ebx,0
int 80h
This works if first second and sum numbers are only 1 digits.But how to do with multiple digits?
« Last Edit: September 25, 2016, 07:11:59 AM by Frank Kotler »

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: Adding user input.
« Reply #1 on: September 25, 2016, 08:15:28 AM »
Hi shadyPengi.

Welcome to the forum. I edited your post just to put "code tags" around your code - the word "code" in square brackets (like a memory reference) at the beginning and "/code" at the end. Might make it easier to read and definitely makes it easier to cut and paste.

This is about the most frequently asked question of all time, and I'm sorry we don't have a standard answer to it.

Some discussion on the subject here:
https://forum.nasm.us/index.php?topic=1514.msg6228#msg6228
As you can see, some people prefer using the C library. Surely it is easier - someone else has already written your code. Tested it and perhaps optimized it better than your routine or mine will ever be, too. So why bother with assembly language at all?

If you "like" assembly language or prefer to "do it yourself", there's a little more discussion (not much) in this recent post:
https://forum.nasm.us/index.php?topic=2267.msg10075#msg10075

You've alreay got the idea that the difference between a number and the (ascii) character representing that number is 48 or 30h or (more "self documenting") '0'. Each "place" in a "decimal" (base 10) number represents a multiple of 10. Work it out from there...

I can help you more, but I would prefer you to write your own code. If you really want someone to write it for you - use C!

Best,
Frank

I should point out that "stressful" has a library:
https://forum.nasm.us/index.php?topic=2269.0

and John Found has another:
https://forum.nasm.us/index.php?topic=2264.0
which you can use... either use the routines, or use them for ideas how to do it.