0

The code below is to add two numbers that are in memory, defined in section of data,but also the result is in memory without a specific value. So at the end of the proccess of sum, the code call sys_write first to print the message and then again with sys_write print the result. But in terminal I got this :

La suma es
1234567890
1098765432
2333333322
2333333322
�U

The code is :

section .data
   sum_msg: db "La suma es", 10
   num1:  db "1234567890",10
   num2:  db "1098765432",10
   sum:   db "          ", 10
   lenmsg: equ $-sum_msg
   lensum: equ $-num1

section .text
global _start
_start:

mov esi, 9
mov ecx, 10
clc
add_loop:
    mov al, [num1+esi]
    adc al, [num2+esi]
    aaa
    pushf
    or al, 30h
    popf
    mov [sum+esi], al
    dec esi
    loop add_loop


mov ecx, sum_msg
    mov edx, lenmsg
    mov ebx, 1
    mov eax, 4
    int 0x80     ; call kernel



    mov ecx, sum
    mov edx, lensum
    mov ebx, 1
    mov eax, 4
    int 0x80     ; call kernel


    mov eax, 1
    int 0x80

Thanks and I hope someone could help me to understand why this is happening.

Soichiru
  • 57
  • 1
  • 6
  • You have your lengths defined wrong. `lenmsg` includes all the text and `lensum` is also more than what you want. – Jester Sep 25 '19 at 16:02

2 Answers2

1

Jester is correct. To elaborate, the $ is the current location. The way that you have lenmsg and lensum declared is equivalent to:

lenmsg: equ lenmsg-sum_msg

lensum: equ lensum-num1

I would have expected the second sys_write to print from num1 to the end of sum plus lenmsg interpreted as characters (i.e. the last line of the output that you provided.)

0

Thank you for your answers Jester and Paul, I forgot to take in count the correct order for the code, now I have this :

  sum_msg: db 'La suma es', 10
  lenmsg: equ $-sum_msg
  num1:  db '1234567890', 10
  lensum: equ $-num1
  num2:  db '1098765432', 10
  sum:   db "          ", 10

And the result is :

  La suma es
  2333333322
Soichiru
  • 57
  • 1
  • 6