0

The purpose of this program is to divide 36 by 10 and print the remainder (6) to the screen. The code I specified below doesn't work. I figured out that it was because on my marked line I was putting the value itself into ecx and not the address of that value in memory.

I later changed the code so that it first puts the value into a variable, and then did mov ecx, variable (which actually puts the address of the variable, not the value onto the register ecx). And it worked.

  1. Why doing mov ecx, variable puts variables's address into ecx's value, but doing mov ecx, edx puts edx's value into ecx value?

  2. How can I specify the address of a register and put it in another register's value?

global _start

section .text
_start:
    mov ebp, esp; for correct debugging
    
    ; division
    mov eax,36
    mov ebx,10
    mov edx,0   ; cleaning edx before division
    div ebx
    add edx,0x30 ; converting the remainder (integer) into ascii digit
    
    ; display result
    mov eax,4
    mov ebx,1
    mov ecx,edx        ; pay attention to this line
    mov edx,1
    int 80h
    
    ; exit
    mov eax,1
    mov ebx,0
    int 80h
Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
rimoto
  • 21
  • 6
  • 3
    Registers are not addressible in any way. – ecm Dec 30 '22 at 22:36
  • 2
    1. Because that's the syntax the NASM designers chose. Other assemblers may do it differently. For example, with gas, `mov ecx, variable` does load the **value** of `variable`; it requires $ before the variable name to load its address. 2. You cannot. – prl Dec 30 '22 at 23:14
  • @prl: in GAS `.intel_syntax noprefix`, you need `mov ecx, OFFSET variable`. A `$` only applies to AT&T syntax, like `mov $variable, %ecx`. (GAS assembles `.intel_syntax noprefix; mov ecx, $variable` as a load from a symbol whose name includes a dollar sign, with the relocation entry being `R_X86_64_32S $variable`) – Peter Cordes Dec 31 '22 at 03:24
  • 2
    Re: NASM symbols and why it makes sense that a non-register symbol uses the address instead of loading from it: [Is every variable and register name just a pointer in NASM Assembly?](https://stackoverflow.com/q/71543020) makes the analogy to labels being like C `char variable[];`. You could think of other things (registers and `equ` constants) as being actual values, though. – Peter Cordes Dec 31 '22 at 03:28

0 Answers0