Author Topic: Simple add num1+num2=num1, whats going on?  (Read 6477 times)

Offline emisaeljcr

  • Jr. Member
  • *
  • Posts: 3
Simple add num1+num2=num1, whats going on?
« on: April 26, 2011, 08:31:52 PM »
Hi everyone, i comeback with a post from @monsterhunter445,
im with the same practice but my output give me only the num1...

sorry for my english ^^, i only speak spaƱish.... ^^

you can see that it is a simple add, num1+num2=resul (strings); intn1+intn2=iresul (integers)... :3 then the output give me always num1.

Code: [Select]
***************************************************************
;nasm -f elf suma.asm
;ld -s -o suma suma.o
;./suma


SECTION .data
msg1: db "Este programa realiza la suma de 2 numeros",10
len1: equ $-msg1

msg2: db "introduzca el primer numero: ",10
len2: equ $-msg2

msg3: db "introduzca el segundo numero: ",10
len3: equ $-msg3

msg4: db "El resultado de la suma es: ",10
len4: equ $-msg3

SECTION .bss
num1: resb 255
num2: resb 255
intn1: resb 255
intn2: resb 255
iresul: resb 255
resul: resb 255

SECTION .text
        global _start
_start:

;msj1
mov edx,len1 ; arg3, length of string to print
mov ecx,msg1 ; arg2, pointer to string
mov ebx,1 ; arg1, where to write, screen
mov eax,4 ; write sysout command to int 80 hex
int 0x80 ; interrupt 80 hex, call kernel

;msj2
mov edx,len2 ; arg3, length of string to print
mov ecx,msg2 ; arg2, pointer to string
mov ebx,1 ; arg1, where to write, screen
mov eax,4 ; write sysout command to int 80 hex
int 0x80 ; interrupt 80 hex, call kernel

;introducir num1
mov edx,255
mov ecx,num1
mov ebx,0
mov eax,3
int 80h

;msj3
mov edx,len3 ; arg3, length of string to print
mov ecx,msg3 ; arg2, pointer to string
mov ebx,1 ; arg1, where to write, screen
mov eax,4 ; write sysout command to int 80 hex
int 0x80 ; interrupt 80 hex, call kernel

;introducir num2
mov edx,255
mov ecx,num2
mov ebx,0
mov eax,3
int 80h

;msj4
mov edx,len4 ; arg3, length of string to print
mov ecx,msg4 ; arg2, pointer to string
mov ebx,1 ; arg1, where to write, screen
mov eax,4 ; write sysout command to int 80 hex
int 0x80

;convertir num1 a entero
mov ebx, [num1]
sub ebx, 48             ;can someone say me why sub 48 to the num1 value? i know that is to convert but
mov [intn1], ebx                                                                                                                     why subtract 48?

;convertir num2 a entero
mov ebx, [num2]
sub ebx, 48
mov [intn2], ebx

;sumar enteros
mov eax, [intn1]
add eax, [intn2]
mov [iresul], eax

;convertir iresul en string                                   i dont know completely this conversion...
mov edx,10
xor edx,edx
div ebx
add dl, '0'
mov [resul],dl

;resultado
mov edx,255
mov ecx,[iresul]
mov bx,1
mov eax,4
int 80h

;terminar
mov ebx,0 ; exit code, 0=normal
mov eax,1 ; exit command to kernel
int 0x80 ; interrupt 80 hex, call kernel

Edit: Please use Code Blocks and don't Double Post questions.
« Last Edit: April 27, 2011, 01:29:20 AM by Keith Kanios »

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2505
  • Country: us
Re: Simple add num1+num2=num1, whats going on?
« Reply #1 on: April 28, 2011, 12:36:12 AM »
It's a little subtle, actually. You've got the right idea, but made a couple of typos. The first one calculates the length of msg4 as "$ - mag3". This causes your attempt to print msg4 to print too much... running into num1, and printing that! That's why you get the first number every time!

The second typo is where you're converting the sum back to an ascii digit. You're preparing to "div ebx", and you want it to be 10... but you put the 10 in edx, not ebx.

Then... when you get to printing the result, you appear to be putting the number to print in ecx. You've used this same interrupt several times, so you know that what goes in ecx is the address of the buffer (or "pointer to string")... and you've just put the sum, converted back to an ascii character, into such a buffer. I don't know if this counts as a "typo" or a "thinko"! :)

Code: [Select]
;***************************************************************
;nasm -f elf suma.asm
;ld -s -o suma suma.o
;./suma


SECTION .data
msg1: db "Este programa realiza la suma de 2 numeros",10
len1: equ $-msg1

msg2: db "introduzca el primer numero: ",10
len2: equ $-msg2

msg3: db "introduzca el segundo numero: ",10
len3: equ $-msg3

msg4: db "El resultado de la suma es: ",10
; len4: equ $-msg3 ; typo!!!
len4: equ $-msg4

SECTION .bss
num1: resb 255
num2: resb 255
intn1: resb 255
intn2: resb 255
iresul: resb 255
resul: resb 255

SECTION .text
        global _start
_start:

;msj1
mov edx,len1 ; arg3, length of string to print
mov ecx,msg1 ; arg2, pointer to string
mov ebx,1 ; arg1, where to write, screen
mov eax,4 ; write sysout command to int 80 hex
int 0x80 ; interrupt 80 hex, call kernel

;msj2
mov edx,len2 ; arg3, length of string to print
mov ecx,msg2 ; arg2, pointer to string
mov ebx,1 ; arg1, where to write, screen
mov eax,4 ; write sysout command to int 80 hex
int 0x80 ; interrupt 80 hex, call kernel

;introducir num1
mov edx,255
mov ecx,num1
mov ebx,0
mov eax,3
int 80h

;msj3
mov edx,len3 ; arg3, length of string to print
mov ecx,msg3 ; arg2, pointer to string
mov ebx,1 ; arg1, where to write, screen
mov eax,4 ; write sysout command to int 80 hex
int 0x80 ; interrupt 80 hex, call kernel

;introducir num2
mov edx,255
mov ecx,num2
mov ebx,0
mov eax,3
int 80h

;msj4
mov edx,len4 ; arg3, length of string to print
mov ecx,msg4 ; arg2, pointer to string
mov ebx,1 ; arg1, where to write, screen
mov eax,4 ; write sysout command to int 80 hex
int 0x80

;convertir num1 a entero
mov ebx, [num1]
sub ebx, 48             ;can someone say me why sub 48 to the num1 value?
                                           ; i know that is to convert but why subtract 48?
mov [intn1], ebx

;convertir num2 a entero
mov ebx, [num2]
sub ebx, 48
mov [intn2], ebx

;sumar enteros
mov eax, [intn1]
add eax, [intn2]
mov [iresul], eax

;convertir iresul en string                                   i dont know completely this conversion...
; mov edx,10 ; typo!
mov ebx,10
xor edx,edx
div ebx
add dl, '0'
mov [resul],dl

;resultado
; mov edx,255 ; really too many...
mov edx, 1  ; we only "converted" one digit->character!
; mov ecx,[iresul] ; no, no, no...
mov ecx, resul   ; address(!) of buffer
mov bx,1
mov eax,4
int 80h

;terminar
mov ebx,0 ; exit code, 0=normal
mov eax,1 ; exit command to kernel
int 0x80 ; interrupt 80 hex, call kernel

That'll work for two single-digit inputs... whose sum is also a single digit. If you want to work with more than one digit, you'll need more complicated conversion routines. You've got a good start...

Why 48? Good question. If you do "man ascii" and scroll down to 48 (decimal representation is in the second column), you'll see it represents the character '0'. What we get from the user ("strings"), and what we want to print at the end are ascii characters, which are not the same as the number they represent. The difference is 48 decimal... or 0x30 or 30h hex... or octal 60q (the ascii chart reminds me)... or just " '0' "! If you write it as " '0' " (you do, in one place) it might be clearer "Why 48?"...

We can help with the multiple-digit conversion routines, but try it on your own. Even if it isn't "homework" it's good practice... and you might come up with the best one yet!

Best,
Frank


Offline emisaeljcr

  • Jr. Member
  • *
  • Posts: 3
Re: Simple add num1+num2=num1, whats going on?
« Reply #2 on: April 28, 2011, 03:33:22 AM »
juhhh! Thanks Frank!

i can see why was not working, i made the changes, run perfectly but as you answered me it only will show 1 character... mmmm so i should research to make it for more than 1 character and be float ^^ i will try it by myself, as you say its a homework but little biggest that it.... :CCC

i should make a unit converter, it should be like google converter, but i have not idea about make the graphics and make use of the mouse... :CC