News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

registers not changing

Started by asymptote, January 11, 2008, 01:05:52 PM

Previous topic - Next topic

asymptote

I'm following a book exercise and it requests the following:

Write assembly code to exchange the values of word variables A and B using only the mov instruction and two registers.

So I'm just going to watch the registers in my debugger - using Masm 6.15 - and here's what I have:


.model  small

.stack  100h

.data

a dw 1111h
b dw 2222h

        .code

hello   proc
        mov     ax, @data
        mov     ds, ax

mov ax, a
mov bx, b
mov a, bx
mov b, ax
mov a, ax
mov b, bx

        _exit   0
       
hello   endp

        end     hello


I'm watching the registers in the debugger and the values of a and b do successfully go into the registers.  I'm not printing their values so I move the exchanged values of a and b back into the ax and bx registers just so I can see they were exchanged, but they're retaining the initial values of a and b, respectively.  What am I doing wrong?

Tight_Coder_Ex

The register are as follows

eax -> ax -> ah & al
ecx -> cx -> ch & cl
edx -> dx -> dh & dl

These are the only three general purpose registers that have 16 & 8 bit abilities
I'm surprised your code even compiled as with Ollydbg I can't cohere it to do what you've written

Sarel

I'm not printing their values so I move the exchanged values of a and b back into the ax and bx registers just so I can see they were exchanged,

You did not do that.

ossama

just as Tight_Coder_Ex said, you have to use general purpose registers,ie: eax,ecx,edx

asymptote

Quote from: Sarel on January 11, 2008, 03:44:36 PM
I'm not printing their values so I move the exchanged values of a and b back into the ax and bx registers just so I can see they were exchanged,

You did not do that.

Yes, I realized that I moved values OUT of the registers, not back into them.  Foolish me.  I got it to assemble - it worked - and I verified that I was properly exchanging the values of A and B.  Thanks for your help.

Tedd

Tight_Coder & Ossama: it's 16-bit code, and it can be assembled fine (note the "model .small") and then linked into a .com file

asymptote: just to clear up what I'm guessing is a misunderstanding in your thinking - 'mov' only copies the value from one reg/var to another, the original value in the source still remains. So..


mov ax, a    ;copy the value of the variable 'a' (1111h) into ax {ax = 1111, a = 1111}
mov bx, b    ;copy the value of variable 'b' (2222h) into bx {bx = 2222, b = 2222}
mov a, bx    ;copy bx's value (2222h) into 'a' {a = 2222, bx = 2222}
mov b, ax    ;copy ax's value (1111h) into 'b' {b = 1111, ax = 1111}
;; at this point the values of 'a' and 'b' have now been exchanged -- a = 2222, b = 1111
mov a, ax    ; ...copy ax (1111) back into a {a = 1111}
mov b, bx    ; ...copy bx (2222) back into b {b = 2222}
;; now a and b have been restored back to their original values, which is probably why they appeared unchanged
No snowflake in an avalanche feels responsible.

evlncrn8

Quote from: Tight_Coder_Ex on January 11, 2008, 03:32:06 PM
The register are as follows

eax -> ax -> ah & al
ecx -> cx -> ch & cl
edx -> dx -> dh & dl

These are the only three general purpose registers that have 16 & 8 bit abilities
I'm surprised your code even compiled as with Ollydbg I can't cohere it to do what you've written

erm and ebx went on a party and never came back?

Tight_Coder_Ex

I stand corrected on two accounts and my excuses are as follows:

(1) I'm not accustomed to seeing single letter variable names, but that does not excuse not reading the code entirely.

(2) Technically IA32 has 8 general purpose registers, but as EBX leans a little more toward special purpose and M$ requires in not be destroyed so I've just become accustomed to considering it special purpose.

There, I feel better now that I've rationalized my shortcomings.

asymptote

Quote from: Tedd on January 11, 2008, 08:16:48 PM

asymptote: just to clear up what I'm guessing is a misunderstanding in your thinking - 'mov' only copies the value from one reg/var to another, the original value in the source still remains. So..


mov ax, a    ;copy the value of the variable 'a' (1111h) into ax {ax = 1111, a = 1111}
mov bx, b    ;copy the value of variable 'b' (2222h) into bx {bx = 2222, b = 2222}
mov a, bx    ;copy bx's value (2222h) into 'a' {a = 2222, bx = 2222}
mov b, ax    ;copy ax's value (1111h) into 'b' {b = 1111, ax = 1111}
;; at this point the values of 'a' and 'b' have now been exchanged -- a = 2222, b = 1111
mov a, ax    ; ...copy ax (1111) back into a {a = 1111}
mov b, bx    ; ...copy bx (2222) back into b {b = 2222}
;; now a and b have been restored back to their original values, which is probably why they appeared unchanged


The only reason I was moving the values back into the registers is because I didn't know how to view the contents of a variable in debug mode.  To verify that the contents of A and B had been swapped, I moved their values back into ax and bx and then viewed them with the -r feature of debug.  After emailing my instructor he explained how I can view values in the ds and avoid this step in the future.  Thanks for all your help!

raymond

Quote from: Tight_Coder_Ex on January 12, 2008, 01:34:00 AM
but as EBX leans a little more toward special purpose and M$ requires in not be destroyed

That may be only partly true, specially for Windows versions prior to WinXP. If your app does not interact with the OS (such as when you create a window and wait for input from a user), the EBX, ESI, EDI and even EBP can be used freely without preserving them. I've done it numerous times when writing small apps geared to solving some math problems with a simple display of an answer in a message box before terminating.

However, I've seen Win98 crash when the ESI and EDI registers had been inadvertently switched in a major program although that same program was running perfectly without a hitch under WinXP.

Raymond
When you assume something, you risk being wrong half the time
http://www.ray.masmcode.com

dsouza123

Some variations for exchanging values in variables:

; two registers used, original values of both ax, bx lost
mov  ax, a
mov  bx, b
mov  b, ax
mov  a, bx

; one register used, original value of ax lost
mov  ax, a
xchg ax, b
mov  a, ax

; one register used, original value of ax retained
xchg ax, a
xchg ax, b
xchg ax, a

; no  registers used, stack used temporarily
push a
push b
pop  a
pop  b

asymptote

Quote from: dsouza123 on January 12, 2008, 11:16:04 PM
Some variations for exchanging values in variables:

; two registers used, original values of both ax, bx lost
mov  ax, a
mov  bx, b
mov  b, ax
mov  a, bx

; one register used, original value of ax lost
mov  ax, a
xchg ax, b
mov  a, ax

; one register used, original value of ax retained
xchg ax, a
xchg ax, b
xchg ax, a

; no  registers used, stack used temporarily
push a
push b
pop  a
pop  b

That's right out of my book (mentioned above).  Do you have it?