Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.os.msdos.programmer > #4050

Re: assembly: mov a memory word into register

From Mateusz Viste <mateusz@xyz.invalid>
Newsgroups comp.os.msdos.programmer
Subject Re: assembly: mov a memory word into register
Date 2021-11-15 09:31 +0100
Organization . . .
Message-ID <20211115093118.300aaae9@mateusz.lan> (permalink)
References <20211112143535.05889a42@mateusz.lan> <smsegf$308$1@gioia.aioe.org>

Show all headers | View raw


2021-11-14 at 20:53 -0500, Rod Pemberton wrote:
> You might need to declare "buff" as a C variable, with both a
> "volatile" keyword and as a pointer.  E.g., perhaps (untested):
> 
>  volatile unsigned char *buff=0xb800;
> 
> Then in the _asm{} section, the compiler should recognize "buff" as a
> C variable, use the assigned address, and not optimize away the code
> (due to the volatile):
> 
>  mov ax,[buff]

I'm sorry but I fail to see what problem you are trying to solve here.
The compiler already knows that buff is a C variable and this variable
can be used from within an inline asm block even without volatile.

Herbert's reply made me realize that buff is neither a register nor a
constant address, hence it cannot be used as indirect addressing. I was
too eager to apply the assembly concept of a "variable" (which is, in
fact, just a pre-calculated address offset) to a C pointer.

The fact that "mov r, [buff]" and "mov r, buff" are equivalent for wasm
didn't help to clear my initial confusion.

Consider this program:

#include <i86.h>
#include <stdio.h>

int main(void) {
  unsigned char *buff = "AB";
  unsigned short mybx = 0, mycx = 0, mydx = 0;

  _asm {
    mov cx, buff
    mov dx, [buff]

    mov bx, buff
    mov bx, [bx]

    mov mybx, bx
    mov mycx, cx
    mov mydx, dx
  }

  printf("mybx=%04X mycx=%04X mydx=%04X (buff=%p)\r\n",
          mybx, mycx, mydx, buff);
  return(0);
}

and its output:

mybx=4241 mycx=0022 mydx=0022 (buff=022)


Your volatile suggestion could be a solution if mybx, mycx, mydx were
optimized away as zeroes, but that's not a problem that exists in this
context. Here the problem was that I naively wanted to obtain the
0x4241 ("AB") result without using an intermediary register for indirect
addressing.

Both C and assembly can be tricky, but both mixed together lead to
entirely new classes of troubles.

Mateusz

Back to comp.os.msdos.programmer | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

assembly: mov a memory word into register Mateusz Viste <mateusz@xyz.invalid> - 2021-11-12 14:35 +0100
  Re: assembly: mov a memory word into register "Kerr-Mudd, John" <admin@127.0.0.1> - 2021-11-12 16:52 +0000
  Re: assembly: mov a memory word into register Herbert Kleebauer <klee@unibwm.de> - 2021-11-12 17:53 +0100
    Re: assembly: mov a memory word into register Mateusz Viste <mateusz@xyz.invalid> - 2021-11-12 18:40 +0100
      Re: assembly: mov a memory word into register Herbert Kleebauer <klee@unibwm.de> - 2021-11-12 19:19 +0100
        Re: assembly: mov a memory word into register Mateusz Viste <mateusz@xyz.invalid> - 2021-11-12 20:07 +0100
          Re: assembly: mov a memory word into register Herbert Kleebauer <klee@unibwm.de> - 2021-11-12 22:18 +0100
      Re: assembly: mov a memory word into register rridge@csclub.uwaterloo.ca (Ross Ridge) - 2021-11-15 16:53 +0000
  Re: assembly: mov a memory word into register Rod Pemberton <noemail@basdxcqvbe.com> - 2021-11-14 20:53 -0500
    Re: assembly: mov a memory word into register Mateusz Viste <mateusz@xyz.invalid> - 2021-11-15 09:31 +0100
      Re: assembly: mov a memory word into register Herbert Kleebauer <klee@unibwm.de> - 2021-11-15 12:10 +0100
        Re: assembly: mov a memory word into register Mateusz Viste <mateusz@xyz.invalid> - 2021-11-15 13:02 +0100

csiph-web