News:

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

how to subtract in masm

Started by xerox, March 07, 2011, 03:17:35 AM

Previous topic - Next topic

xerox

I have a question regarding subtraction in masm. Actually I am trying to do some program in masm using irvine library. The following code gives me a some junk value which I didn't expected. How can I output negative value in the following code. I will really appreciated for your help.

INCLUDE Irvine32.inc

.data
x DWORD 15
y DWORD 20
z DWORD 2
result DWORD ?

.code

main PROC
     mov eax,x
     mov ebx,y
     sub eax,ebx        ;subtract 15-20
     mov ecx,z           ;copy 2 to ecx
     mul ecx              ;multiply -5*2 = -10
     call WriteDec       ;output -10
exit
main ENDP
end main

     

MichaelW

WriteDec writes an unsigned 32-bit decimal number to standard output. The displayed value, 4294967286, is not "some junk value" but -10 interpreted as an unsigned value. The Irvine32 WriteInt procedure writes a 32-bit signed binary integer to standard output.


eschew obfuscation

xerox

thank you michael, really appreciated for your help. I have one more question is it possible to use WriteDec instead of WriteInt because it still shows +4294967286.

MichaelW

It works for me.


include Irvine32.inc
includelib irvine32.lib
includelib \masm32\lib\kernel32.lib

.data
  x DWORD 15
  y DWORD 20
  z DWORD 2
.code

start:

    mov eax, x
    mov ebx, y
    sub eax, ebx        ;subtract 15-20
    mov ecx, z          ;copy 2 to ecx
    mul ecx              ;multiply -5*2 = -10
    push eax
    call WriteDec       ;output -10
    call crlf
    pop eax
    call WriteInt
    call crlf
    call WaitMsg
exit
end start


4294967286
-10
Press [Enter] to continue...

eschew obfuscation

xerox

Thankyou once again Michael. Your help helps me alot I really appreciated. But still have one more question regarding the same question(subtraction). Actually I am trying to get an idea from giving you a small program so that I can get an idea and jump in my crazy program. Still doesn't work in my program. My actual program is trying to do some subtraction and division. For e.g.
   -250/100 = -2.50
I don't have any idea about floating points so I have to use long method I guess. I am using character "." in between the quotient and remainder.
I want the quotient and remainder as -2.50 and display as it is. so do you have any idea about that. I will really appreciated for your help.

MichaelW

AFAIK the Irvine components provide no way to display floating-point numbers. There was a similar question asked here:

http://www.masm32.com/board/index.php?topic=4794.0

And the OP selected the solution described here:

http://www.masm32.com/board/index.php?topic=4794.msg35979#msg35979


eschew obfuscation

clive

Something along these lines, although learning to do your own homework will serve you better in the future.

    mov eax,-250
    cdq  ; Sign extend, edx:eax = eax
    mov ecx,100
    idiv ecx ; eax = edx:eax/ecx, edx = edx:eax%ecx
; eax = -250/100, edx = -250%100   BOTH will be negative
    push edx
    call PrintInt ; Integer Quotient
    mov x,eax
    mov i,edx
;; PrintDot ; Decimal Point
    pop eax ; rem = -250%100, -50
    cdq           ; eax = abs(rem)
    xor eax,edx
    sub eax,edx
    xor edx,edx   ; edx = 0, for division
    mov ecx,10
    div ecx
; eax = rem/10, edx = rem%10
    push edx
    call PrintDec ; first decimal place
    pop  eax
    call PrintDec ; second decimal place
It could be a random act of randomness. Those happen a lot as well.

Farabi

Hi,
There are something you need to know about data type.
decimal type is called integer, and decimal type divided into 2 type, unsigned data where it had no negative value and signed data where it had negative value.
Floating point type where it is have a fraction value like 0.23456, 1.5 etc.

You need to know about that.
Also, on MASM you will need to convert those binary data before you able to see it printed on a memory. There are many convertion function on the MASM lib before you can see the value printed on the screen.
Those who had universe knowledges can control the world by a micro processor.
http://www.wix.com/farabio/firstpage

"Etos siperi elegi"