News:

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

QWORD math

Started by Bieb, January 23, 2005, 08:56:55 PM

Previous topic - Next topic

Bieb

Now, I know that the dec instruction can only be used for DWORDS, so what if I have two DWORDS that comprise a QWORD?  How would I go about decrementing that value and telling when it had reached 0?

MichaelW

This appears to work OK, but I doubt that it is the best method.

; ««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
    .486                       ; create 32 bit code
    .model flat, stdcall       ; 32 bit memory model
    option casemap :none       ; case sensitive
    include \masm32\include\windows.inc
    include \masm32\include\masm32.inc
    include \masm32\include\kernel32.inc
    includelib \masm32\lib\masm32.lib
    includelib \masm32\lib\kernel32.lib
    include \masm32\macros\macros.asm
; ««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
    .data
        ctr dq 0
    .code
; ««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
start:
; ««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
    xor   ebx,ebx
    mov   DWORD PTR ctr[0], 10h ; init least significant dword
    mov   DWORD PTR ctr[4], 1   ; init most significant dword

  decloop:                      ;;
    sub   DWORD PTR ctr[0], 1   ;; ctr[0] = ctr[0] - 1
    sbb   DWORD PTR ctr[4], 0   ;; ctr[4] = ctr[4] - carry flag

    ; ------------------------------------------------------
    ; This code skips forwards once when ctr[4] reaches 0
    ;  so the test can complete in a reasonable time.
    ; ------------------------------------------------------
    jnz   @F
    inc   ebx
  @@:
    cmp   ebx, 1
    jne   @F
    mov   DWORD PTR ctr[0], 10h
  @@:
    ; ------------------------------------------------------

    print uhex$(DWORD PTR ctr[4])
    print chr$(" ")
    print uhex$(DWORD PTR ctr[0])
    print chr$(13,10)
    mov   eax, input()          ; press enter to continue

    mov   eax, DWORD PTR ctr[0] ;;
    or    eax, DWORD PTR ctr[4] ;;
    jz    @F                    ;; ctr[0] = 0 and ctr[4] = 0
    jmp   decloop               ;;

  @@:
    mov   eax, input("Press enter to exit...")
    exit
; ««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
end start

eschew obfuscation

Ratch

Bieb,

QuoteNow, I know that the dec instruction can only be used for DWORDS,...

     DEC can be used for  a DWORD, WORD, and BYTE.  Ratch

raymond

To make it simple, declare your qword as two dwords. Then:

.data
  qwLow dd "Something"
  qwHi  dd "Something"

.code
  sub qwLow,1
  sbb qwHi,0    ;every time the qwLow would be 0
                ;the "sub qwLow,1" would set the Carry flag
                ;and the "sbb qwHi,0" would then reduce the qwHi by 1
  jc  count_terminated
                ;when both the qwLow and qwHi are 0,
                ;only then will the "sbb qwHi,0" set the Carry flag


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