News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

Subtraction

Started by hfheatherfox07, December 12, 2012, 07:22:40 PM

Previous topic - Next topic

hfheatherfox07

Hi there ....
I found this little code

data segment
var1 dw 1020h
var2 dw 2222h
res dw ?
data ends
code segment
assume cs:code,ds:data
start:
mov ax,data
mov ds,ax
mov ax,var1
add ax,var2
mov res,ax
int 3
code ends
end start


I am having issues converting it to masm32  :(

include \masm32\include\masm32rt.inc

Subtract proto

.data
caption  db "Subtract Example", 0

var1     db "403007", 0
var2     db "400000", 0
buffer   db 32 dup(0)

.code

Start:
invoke Subtract
invoke MessageBox, 0, addr buffer, addr caption,  MB_ICONQUESTION + MB_OK
invoke ExitProcess, 0
Subtract Proc

pushad

lea esi, var1
lea edi, var2
mov edi,eax
mov eax,offset var1
sub esi,offset var2

invoke lstrcpy,addr buffer, eax

popad

xor eax,eax
ret

Subtract endp
end Start


Thank You!
Your code and your skills will be assimilated. Your programming language is irrelevant.
We are the ASM Borg and you will become part of us. Compile and be assembled.

dedndave

the code example is 16-bit - i guess you understand that

but - the problem you are facing is that the code example adds 2 binary values
your code - you have 2 ASCII Decimal Strings

now, you could use BCD or "packed decimal" math to add them
which is a fast solution - but really only fast for add and subtract
if you later want to introduce multiplication or any other more advanced math, BCD is not so fast

the best solution is to convert the ASCII decimal strings into binary values, first
then, you can add, subtract, or whatever

or...
use binary values to start with   :biggrin:

var1     dd 403007
var2     dd 400000


it is good to see you hit the basics, though   :t

hfheatherfox07

Thanks dedndave  :biggrin:

It is good to brush up on the basics sometimes .....I find I do so many other things I forget the simple stuff ::)
Took another look at chapter 3 from Kip R. Irvine's Assembly Language for Intel-Based Computers, 4th Edition

include \masm32\include\masm32rt.inc

Subtract proto

.data
caption  db "Subtract Example", 0
Format   db "%x",0
var1     dd 403007h ; binary values NOT  ASCII Decimal Strings
var2     dd 400000h ; binary values NOT  ASCII Decimal Strings
buffer   db 32 dup(0)

.data?
finalVal dword  ?
.code

Start:
invoke Subtract
invoke MessageBox, 0, addr buffer, addr caption,  MB_ICONQUESTION + MB_OK
invoke ExitProcess, 0
Subtract Proc

mov eax,var1
sub eax,var2
mov finalVal,eax ; store the result
invoke wsprintf, addr buffer, addr Format, eax

xor eax,eax
ret

Subtract endp
end Start
Your code and your skills will be assimilated. Your programming language is irrelevant.
We are the ASM Borg and you will become part of us. Compile and be assembled.

dedndave


hfheatherfox07

Quote from: dedndave on December 13, 2012, 08:20:49 AM
it should display "3007"

Yes , it does here.... :t
I just posted that code as a solution, incase some wanted it  :biggrin:
Your code and your skills will be assimilated. Your programming language is irrelevant.
We are the ASM Borg and you will become part of us. Compile and be assembled.

dedndave

these type of programs are known as "trivials"
you might find it simpler to just use the console...

        INCLUDE    \masm32\include\masm32rt.inc

        .CODE

_main   PROC

        mov     eax,403007h
        sub     eax,400000h
        print   uhex$(eax),13,10

        inkey
        exit

_main   ENDP

        END     _main


assemble as a console app

ValliereAllen

.386
.MODEL FLAT, STDCALL
OPTION CASEMAP:NONE
INCLUDE C:\MASM32\INCLUDE\windows.inc
INCLUDE C:\MASM32\INCLUDE\kernel32.inc
INCLUDE C:\MASM32\INCLUDE\masm32.inc

INCLUDELIB C:\MASM32\LIB\kernel32.lib
INCLUDELIB C:\MASM32\LIB\masm32.lib



.Data
    msg1 db 'Enter the value of Subtrahend = ', 0
    msg2 db 'Enter the value of Minuend = ', 0
    msg3 db 0DH, 0AH
    msg4 db 'The Difference = ', 0

.Data?
    Y db 6 DUP(?)     ;ibuf

    S DB 6 DUP(?)       ; inputs
    B DB 6 DUP(?)

    D db 6 DUP(?)         ; stands for the difference
       
    X DB 7 DUP(?)     ;abuf


.code
start:

invoke StdOut, ADDR msg1
invoke StdIn, ADDR Y, SIZEOF Y

MOV AL, Y
SUB AL, 30H
MOV S, AL

MOV AL, Y+1
SUB AL, 30H
MOV S+1, AL

MOV AL, Y+2
SUB AL, 30H
MOV S+2, AL

MOV AL, Y+3
SUB AL, 30H
MOV S+3, AL

invoke StdOut, ADDR msg2
invoke StdIn, ADDR Y, SIZEOF Y

MOV AL, Y
SUB AL, 30H
MOV B, AL

MOV AL, Y+1
SUB AL, 30H
MOV B+1, AL

MOV AL, Y+2
SUB AL, 30H
MOV B+2, AL

MOV AL, Y+3
SUB AL, 30H
MOV B+3, AL


MOV AL, S+3             ;start of the subtraction
SUB AL, B+3
AAS
MOV D+3, AL

MOV AL, S+2
SBB AL, B+2
AAS
MOV D+2, AL

MOV AL, S+1
SUB AL, B+1
AAS
MOV D+1, AL

MOV AL, S
SBB AL, B
AAS
MOV D, AL


Mov AL, X
ADD AL, 30H
Mov D, AL

Mov AL, X+1
ADD AL, 30H
Mov D+1, AL

Mov AL, X+2
ADD AL, 30H
Mov D+2, AL

Mov AL, X+3
ADD AL, 30H
Mov D+3, AL

Mov X+4, 0

        INVOKE StdOut, ADDR msg3
        INVOKE StdOut, ADDR D
        INVOKE ExitProcess, 0

end start



please help me, I am new to this programming lang. The ans. is always 0000. I dunno why . please

jj2007

You need to convert D into a string:

        INVOKE StdOut, ADDR msg3
.data?
somebuffer db 100 dup(?)

.code
movzx eax, D
invoke dwtoa, eax, addr somebuffer

INVOKE StdOut, ADDR somebuffer