0

I'm very beginner in Assembly, And I'm trying to write a program that find the square root of a perfect square root number . my variables does not change when I move something immediately, check variables Box in screen shots

my code:

.data
number dw ? 
result dw ?
.code
    main proc
         
        mov [number],400
        mov cx,0
        
        calc:
        mov ax,cx
        mul cx
        cmp ax,[number]
        jz save
        inc cx
        jmp calc
        
        
        
        save:
        mov [7102],cx
        
        
        
        
        
        hlt
       
    endp
    end main
         

enter image description here

  • 3
    Sounds like segment issues. Not sure about how emu8086 sets up the segments, you might need the usual `mov ax, @data; mov ds, ax` to set it up. – Jester Mar 28 '22 at 20:29
  • I monitor the program step by step , I think var window does not work , variables are ok , at the end the cx value is 14(= 20 in decimal) and its Ok and fine – Danial Hamedi Mar 28 '22 at 20:41
  • 1
    I'm more familiar in GAS, so please correct me if i'm wrong. Don't you need to move 400 into the location of number? not the value of it itself? "Mov [number],400" seems to be moving 400 to the address of what's in "number". I think "Mov number,400" is the correct way? Again, I very well might be wrong as Intel and GAS are a little different. – Sir Archibald Humphrey Mar 28 '22 at 20:42
  • Thanks a Lot, but I tried all the methods, such as : mov [7024],400 / mov [number],400/mov 7024,400 etc – Danial Hamedi Mar 28 '22 at 20:45
  • @quandaledingle `mov [number], 400` translated to C is `*number = 400;`, and as you might know, labels in assembly are addresses (pointers). – xiver77 Mar 29 '22 at 00:47
  • @xiver77 Oh my bad, in at&t it's a little different. Thanks for the clarification though! :) – Sir Archibald Humphrey Mar 29 '22 at 02:28
  • @quandaledingle: symbols/labels in AT&T syntax actually work a lot like MASM syntax (and GAS `.intel_syntax`), where `mov number, 400` is also a store. But with `number: dw ?` magically implying an operand size in MASM, unlike GAS). MASM allows square brackets around addressing modes even when no registers are involved, and many consider that good style, although it's optional in that case. (Unlike NASM where it's always required.) See [Confusing brackets in MASM32](https://stackoverflow.com/q/25129743). – Peter Cordes Mar 29 '22 at 03:34
  • x86 machine code doesn't have a memory-indirect addressing mode so there's no asm syntax can ever have something like `mov [ [number] ], 400` that would load a pointer from memory and then store the immediate into that address. I'd hope most assemblers would reject that syntax. – Peter Cordes Mar 29 '22 at 03:36

1 Answers1

0

I accidentally found the answers to my question, in order to see the changes of the variables, you have to save your data segment address in the DS register, for this you can use the following code!

mov AX,[DATA]  ;[DATA] means the address of data segment like &DATA in C 
mov DS,AX

Note that you can not directly store the data segment address in the DS register, because the DS register does not have direct access to memory!

  • If `mov ax, [DATA]` assembles to a memory-source instruction like expected then `mov ds, [DATA]` should also be valid. – ecm Apr 02 '22 at 20:00
  • 1
    Are you *sure* it's `mov ax, [DATA]` rather than `mov ax, DATA`? I think this has to be an immediate constant, otherwise you have a chicken/egg problem trying to load a value from `ds:disp16` when DS hasn't been set correctly. If this works, it's probably because MASM-style syntax will ignore brackets around numeric constants, still doing a mov-immediate instead of loading from memory, @ecm, unlike more consistently-designed asm syntax like NASM. ([Confusing brackets in MASM32](https://stackoverflow.com/q/25129743)). But it's misleading and definitely not a good idea to write it this way. – Peter Cordes Apr 02 '22 at 23:51
  • 1
    But yes, @ecm is correct that if this *was* a load, `mov ds, mem` is a valid form of `mov Sreg, r/m16`. https://www.felixcloutier.com/x86/mov. You certainly can load from memory into DS, you just don't want to here. You can't mov-immediate into an Sreg, and some people have the misconception that the only valid source is a register, apparently including [some developers of early MS-DOS's own drivers](https://retrocomputing.stackexchange.com/questions/24173/why-are-these-dos-console-drivers-wasting-precious-bytes#comment78733_24185). – Peter Cordes Apr 02 '22 at 23:56