4

I am a beginner and writing assembly program to print numbers from 1 to 9 using this code:

section .text    
   global _start                    
_start:                     
   mov ecx,10    
   mov eax, '1'     
l1:    
   mov [num], eax    
   mov eax, 4    
   mov ebx, 1    
   push ecx     
   mov ecx, num            
   mov edx, 1            
   int 0x80     
   mov eax, [num]    
   sub eax, '0'    
   inc eax    
   add eax, '0'    
   pop ecx    
   loop l1      
   mov eax,1             ;system call number (sys_exit)    
   int 0x80              ;call kernel    
section .bss    
num resb 1

Here we have following three statements:

  1. mov [num], eax
  2. mov ecx , num
  3. mov eax, [num]

I want to know why we should use mov ecx,num rather than mov ecx,[num]

Michael Petch
  • 46,082
  • 8
  • 107
  • 198
Sham
  • 71
  • 1
  • 1
  • 5
  • The difference is explained in http://stackoverflow.com/a/33342016/1524450 – Michael Feb 17 '16 at 17:35
  • 2
    Possible duplicate of [Basic use of immediates (square brackets) in x86 Assembly and yasm](http://stackoverflow.com/questions/10362511/basic-use-of-immediates-square-brackets-in-x86-assembly-and-yasm) – Michael Petch Feb 17 '16 at 17:49
  • I wrote up a guide to [x86 addressing modes](http://stackoverflow.com/a/34058400/224132), including covering the difference between NASM and MASM on this bit of syntax: `mov ecx, num` vs. `mov ecx, OFFSET num`. It was an attempt to write up an answer that Qs like this could be linked to as duplicates. – Peter Cordes Feb 17 '16 at 19:12

1 Answers1

10

If you are familiar with C/C++, here is an explanation.

mov ecx, num is equivalent to:

int num;
ecx = & num;

while mov ecx, [num] is equivalent to :

int num;
ecx = num;

Here, the reason for the line mov ecx, num is because you are calling the system function int 0x80, which requires that ecx contains the address of your numbers. So it should be like that.

WhatsUp
  • 1,618
  • 11
  • 21
  • 1
    okay it means we require to specify address of "num" variable in "ecx" and not its value. So the interrupt will print from that memory location viz, "ecx". Sounds perfect. Thank You – Sham Feb 17 '16 at 17:47