0

I am learning Assembly Language (x86) and I need to figure out how to make a register overflow using only variables rather than intermediate values.

I with to overflow the AX register by putting FFFF into a variable, moving that variable into AX, and then incrementing AX. However, I am running into problems. First off, I go to declare a WORD variable like this:

limitReg WORD 0

Then later on in .code I do this:

MOV limitReg, FFFFh
MOV ax, limitReg

However, I get the following error:

Error   1   error MSB3721: The command "ml.exe /c /nologo /Zi     /Fo"Debug\pa2.obj" /Fl"PA2.lst" /I "C:\Irvine" /W3 /errorReport:prompt  /Ta..\..\..\..\..\..\..\Irvine\Examples\ch03\pa2.asm" exited with code 1. C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\V120\BuildCustomizations\masm.targets 50

Can someone please help me out with learning this?

jshapy8
  • 1,983
  • 7
  • 30
  • 60
  • Aren't there any more error messages in the console? Some that point to the actual lines of code that fail to assemble. – Michael Sep 10 '15 at 19:02
  • @Michael Nope. The only message I get is the one stated. – jshapy8 Sep 10 '15 at 19:22
  • In masm you load from memory like `mov ax, [limitReg]`, which loads **from** the address - instead of just loading the address itself. Also, your first instruction just doesn't exist. You can't move values around without involving a register. – Bo Persson Sep 10 '15 at 19:59
  • The assembler should have generated a file `PA2.lst` (considering the command line option `/F1`). Please view the contents of that file; it should provide full details about what went wrong during assembly. – Ruud Helderman Sep 10 '15 at 20:27
  • Please note `FFFFh` must be preceded by a zero (`0FFFFh`); see http://stackoverflow.com/questions/19934064/hexademical-values-in-masm-starting-with-a-letter – Ruud Helderman Sep 10 '15 at 20:55
  • `limitReg WORD 0`, shouldn't that be `limitReg DW 0`? – Ruud Helderman Sep 10 '15 at 20:58
  • 1
    @BoPersson `mov ax, [limitReg]` is NASM syntax (or TASM in ideal mode); MASM does not accept the brackets. And `MOV memory,immediate` has been a valid instruction ever since the original 8086. Please correct me if I'm wrong. – Ruud Helderman Sep 10 '15 at 21:11
  • @Ruud MASM will ignore the brackets. Stylistically it's a good idea to use brackets for memory operands, so they appear different than immediate operands, but MASM makes that determination based on the type of the symbol. – Ross Ridge Sep 11 '15 at 01:03
  • @RossRidge Thanks for the correction. I totally agree brackets are preferable; I guess most of us do. – Ruud Helderman Sep 11 '15 at 10:39

1 Answers1

2

"intermediate values" huh? Did you mean "immediate"? Memory locations are are intermediate values. Using them directly (inc [myvar]) is usually not a good idea. Memory is for long-term storage of variables, or when you run out of registers and need to spill one.

Terminology aside, I think I know what you're trying to do (using 16bit asm for some reason):

section .data
myvar: dw 0FFFFH     ; 2 bytes of storage intialized to -1

section .text
GLOBAL asmfunction
asmfunction:
    ;mov ax, myvar   ; ax = address of myvar
    mov ax, [myvar]  ; ax = contents of myvar
    inc ax
    mov [myvar], ax  ; store back to the global variable
    ret

Actually, in MASM syntax (rather than NASM), mov ax, myvar might still be a load, rather than a mov-immediate with the address. If you want a load/store, it's definitely a good idea to always write it as [addr]. If you want the address, you can use mov ax, offset myvar, which is more efficient than lea ax, [myvar]

Also, more simply:

xor ax, ax   ; zero ax (but not eax)
dec ax       ; unsigned carry from from 0 to 0xffff.
             ;  However, dec doesn't touch CF, only the other flags

It's common enough to talk about "overflowing" an unsigned value, but keep in mind that instructions set the x86 Carry and Overflow flags based on unsigned carry (between 0 and 0xff...) and signed overflow (between MIN_INT and MAX_INT).

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847