0

The given below program adds two numbers 3 and 4 in nasm.Why in this code the eax which contain 3 and ebx which contain 4 are subtracted by 0 ,and result of sum which is stored in eax is added with zero?,code is given below ,while i tried without these add ,and sub by zero lines it shows unexpected result.

section .text
global _start               ;must be declared for using gcc

_start:                     ;tell linker entry point

mov     eax, '3'
sub     eax, '0'
mov     ebx, '4'
sub     ebx, '0'
add     eax, ebx
add     eax, '0'

mov     [sum], eax

mov     ecx, msg
mov     edx, len
mov     ebx, 1          ;file descriptor (stdout)
mov     eax, 4          ;system call number (sys_write)
int     0x80            ;call kernel

mov     ecx, sum

mov     edx, 1
mov     ebx, 1          ;file descriptor (stdout)
mov     eax, 4          ;system call number (sys_write)
int     0x80            ;call kernel

mov     eax, 1          ;system call number (sys_exit)
int     0x80            ;call kernel

section .data

msg     db              "The sum is:", 0xA,0xD

len equ $ - msg

segment .bss

sum resb 1
coddygeek
  • 125
  • 1
  • 12
  • To convert between ASCII characters which you can print, and integers which you can add. http://asciitable.com. – Peter Cordes Jul 06 '18 at 05:34
  • 1
    "eax which contain 3 and ebx which contain 4" wrong. The symbol '3' is not the same as integer number 3. If you have to work with symbolic numbers (such as when reading from a file or user input) subtract with symbol '0' to obtain the real number, otherwise just type in the numbers without 'quotes'. – Havenard Jul 06 '18 at 05:37
  • The first answer on that duplicate link looks like a good explanation of `3` vs. `'3'`. It uses `0x30` instead of `'0'`, which in this case is maybe a good thing to reinforce that it's the ASCII code for 0. – Peter Cordes Jul 06 '18 at 05:41
  • thanx for ur repley. – coddygeek Jul 06 '18 at 08:52

0 Answers0