-3

Use the following variable definitions
Section .data
var1 DB -4,-2,3,1
var2 DW 1000h,2000h,3000h,4000h
var3 DW -16,-42
var4 DW 1,2,3,4,5

What will be the value of the destination operand after each of the following instructions execute in sequence?

Mov edx, var4 ;a

Movzx edx, var2 ;b

Mov edx, [var4+4]; c

Movsx edx, var1 ; d

d. FFFFFFFCh ↕

c. 00000002h ↕

b. 00001000h ↕

a. 00000001h ↕

I don't know how to solve this question.

janw
  • 8,758
  • 11
  • 40
  • 62
loctp
  • 1
  • 2
  • 1
    Things like `movzx edx, var2` make no sense. There's no source size specified, it could be a byte or a word. It's only useful to infer that `varX` means `[varX]`, so this is a MASM dialect. Unless MASM has a default source size for `movzx` et all (but I don't even want to know about it, such a poorly designed dialect). – Margaret Bloom Nov 02 '22 at 13:47
  • 2
    @MargaretBloom: Since [MASM has types](https://stackoverflow.com/questions/25129743/confusing-brackets-in-masm32/25130189#25130189), I presume it remembers that `var2` was defined using `dw` and therefore it assembles `movzx edx, word ptr [var2]`. Though by that same token, I'd think `mov edx, var4` would be an error. – Nate Eldredge Nov 02 '22 at 14:09
  • 2
    @loctp: For homework questions on this site, you should have some indication of your own progress. What do you know that may be relevant? What ideas have you considered that didn't work, and why didn't they? What have you studied? What resources have you consulted? Are there others you could consult, and if you have not tried them, why not? – Nate Eldredge Nov 02 '22 at 14:12
  • 1
    Please also read https://meta.stackoverflow.com/questions/334822/how-do-i-ask-and-answer-homework-questions all the way through. You can make edits to improve your question by using the edit button under your question. – Nate Eldredge Nov 02 '22 at 14:13
  • 3
    Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Nov 02 '22 at 14:38
  • 1
    The answers are given, but I'm not sure I believe them. The first one `mov edx, var4` has a size mismatch: `edx` is 32 bits and var4 is 8 bits, yet uses an un-widening move, so if it assembled without error, I'd expect more in `edx` than `01h`, i.e. `04030201h`, or else, someone forgot to use a widening opcode, `movzx` or `movsx`, which would yield `01h`, the given answer, (a), or else perhaps there is error in transcription somewhere. In any case, I don't think this is solvable as it stands. – Erik Eidt Nov 02 '22 at 15:23
  • 1
    @ErikEidt: You're correct; MASM would refuse to assemble this because of the size mismatch between `var4 dw ...` vs. `mov edx, var4` and `mov edx, [var4+4]`, neither using `dword ptr [var4+4]`. Or other assemblers would refuse to assemble it because of the ambiguous operand-size in `movzx edx, var2` as @Margaret pointed out. (And it can't be NASM or FASM syntax because `var2` would be the address as an immediate.) – Peter Cordes Nov 02 '22 at 20:07
  • @MargaretBloom: That instruction is fine in MASM/TASM because `foo dw ...` *does* declare `foo` as a "variable" with an associated size. As Erik pointed out, the problem is the size-mismatch between EDX and var4 both times it's used! MASM appears to have been designed for people who'd rather have been writing C, but were stuck working on a project in assembly. With all its magic nonsense like `proc uses esi` that will invent a prologue and rewrite your `ret` into an epilogue with pops (and to `ret n` if you tell it to use stdcall and give it a prototype). And its `invoke` directive, and `if` – Peter Cordes Nov 02 '22 at 20:11

1 Answers1

1

I don't know how to solve this question.

  • Use your noggin:
    1. Take a piece of paper.

    2. Draw a strip of boxes, each box representing a Byte, and put the labels on it.

      X           Y   
       

      This is your memory; rightward are ascending addresses. X and Y are some example labels. Now fill the lower boxes with the values you think are correct. Preferably convert everything to hexadecimal first since the answer will expect that. Also this makes it easier to “split” the sequence of Bytes necessary to store e. g. the value −42.

    3. Draw some boxes for the registers used and label them. (OK, here it’s just edx.)

    4. Then “run” the program, i. e. strike and replace the values as you think the instruction at hand performs.

  • The lazy student, however, does this:
    • Post a question to Stackoverflow or other site and hope somebody gives the answer.
    • Write a program with the assembler used here (possibly [some] MASM, I don’t know) and then single-step with a debugger.
Kai Burghardt
  • 1,046
  • 1
  • 8
  • 23
  • The first 2 instructions look like MASM, but two later instructions don't use `dword ptr` to override the size of `var4`. There might not be any mainstream assembler that will assemble this. (I hope there isn't, because it relies on an implicit size for the source of `movsx`, but also uses `var4` without a size override. Can't have it both ways, either `var2` doesn't imply a size or `var4` does. Well I guess you could let registers more strongly imply a size, with "variable" sizes only being a fallback, but that seems the worst of both worlds, less warnings on ambiguity.) – Peter Cordes Nov 02 '22 at 20:14
  • Otherwise yes, 100% agree that plan A should be to assemble and single-step in a debugger while watching registers change, since you already need a development setup that lets you do that if you want to learn assembly language. – Peter Cordes Nov 02 '22 at 20:16
  • @PeterCordes Thanks. I already saw your question comments. I think we can only suppose this code is actually some kind of “pseudo assembly” invented ad-hoc by the instructor of the class loctp is attending, _deliberately_ introducing some incompatibilities to existing mainstream assemblers so plainly executing it does not suffice. – Kai Burghardt Nov 02 '22 at 20:25
  • I guess that's possible, or just out of sloppyness. Presumably the students have been taught some kind of syntax so they'd know that `var4` is the memory at that address, not the label address. But as a Stack Overflow question we don't have that, so it's very weird. I didn't think `section .data` is a valid MASM directive; they use `.data` or `_DATA SEGMENT ...` ([Section syntax with old MASM3 do not reachable data section](https://stackoverflow.com/q/73583087)) so it looks like NASM for the data, but the instructions have to be MASM. – Peter Cordes Nov 02 '22 at 20:35