1
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

I copied this code from here.
I don't understand what next three lines mean:

sub  eax, '0'
sub  ebx, '0'
add  eax, '0'
Sep Roland
  • 33,889
  • 7
  • 43
  • 76
Роман
  • 17
  • 3
  • 1
    For converting from and to ascii. Consult an [ascii table](http://asciitable.com). You will see the digits have codes `0x30-0x39`. – Jester Dec 30 '20 at 12:38

1 Answers1

3

Note that in mov eax,'3' you are moving a character - so the value of eax is 0x33 after this instruction (the ASCII-code of '3').
By subtracting the (again) character '0' (i.e. 0x30) you convert the digit-character to its actual value 0x03.
Similarly by adding '0' you convert back from a one-digit-value to its corresponding character.

In this case of fixed hardcoded values this does not make much sense, but in the example that takes input from the terminal you get only character-input and have to convert to actual numeric values.

Sep Roland
  • 33,889
  • 7
  • 43
  • 76
piet.t
  • 11,718
  • 21
  • 43
  • 52