News:

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

MY ALGORITH CAN'T WORK - POSEO PROBLEMAS

Started by chola, September 28, 2005, 05:20:33 AM

Previous topic - Next topic

chola

IS IN MASM32

THIS CODE DON'T WORK I DON'T KNOW WHY, I ASUME THAT IS THE DIVISION THAT DON'T WORK FINE, CAN ANYBODY CHECK PLEASE MY CODE TO FIND WHAT IS THE ERROR.

THIS PROGRAM DIVIDE TWO NUMBERS
THANKS.


ESTE PROGRAMA ESTA HECHO PARA DIVIDIR DOS NUMEROS PERO NO LO HE PODIDO PONER A FUNCIONAR CORRECTAMENTE, PUEDE ALGUIEN CORREGUIRME PARA QUE FUNCIONE CORRECTAMENTE.

MUCHAS GRACIAS


;«««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««

    .486                                   ; Directiva indicando uso de instrucciones del 80486
    .model flat, stdcall                   ; Directiva indicadora del modelo de memoria 32 bits
    option casemap :none                   ; Directiva para el reconocimiento de mayusculas 
                                           ; y minusculas

    include \masm32\include\windows.inc    ; Siempre debe ser inicializado de primero
    include \masm32\macros\macros.asm      ; Instrucción para la utilización de macros

  ; -----------------------------------------------------------------
  ; Librerias que deben ser utilizada para utilizar funciones
  ; -----------------------------------------------------------------
    include \masm32\include\masm32.inc
    include \masm32\include\gdi32.inc
    include \masm32\include\user32.inc
    include \masm32\include\kernel32.inc

    includelib \masm32\lib\masm32.lib
    includelib \masm32\lib\gdi32.lib
    includelib \masm32\lib\user32.lib
    includelib \masm32\lib\kernel32.lib

    .code                       ; Directiva que indica el inicio el código

; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««

start:                          ; Inicio del programa

    call main                   ; Invocado el procedimiento main

    exit

; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««

main proc


    LOCAL str1:DWORD
    LOCAL str2:DWORD
    LOCAL var1:DWORD
    LOCAL var2:DWORD
    LOCAL rslt:DWORD
    LOCAL text:DWORD
    LOCAL resi:DWORD
    LOCAL rsltvalue:DWORD
   

; ¤=÷=¤=÷=¤=÷=¤=÷=¤=÷=¤=÷=¤=÷=¤=÷=¤=÷=¤=÷=¤=÷=¤=÷=¤=÷=¤=÷=¤=÷=¤=÷=¤=÷=¤=÷=¤
    LOCAL eax1:DWORD
    LOCAL ebx1:DWORD
    LOCAL ecx1:DWORD
    LOCAL edx1:DWORD
; ¤=÷=¤=÷=¤=÷=¤=÷=¤=÷=¤=÷=¤=÷=¤=÷=¤=÷=¤=÷=¤=÷=¤=÷=¤=÷=¤=÷=¤=÷=¤=÷=¤=÷=¤=÷=¤



    LOCAL buffer1[32]:BYTE
    LOCAL buffer2[32]:BYTE
    LOCAL buffer3[32]:BYTE

    mov str1, ptr$(buffer1)
    mov str2, ptr$(buffer2)
    mov rsltvalue, ptr$(buffer3)


    cls
    loc 10, 6
    print "DIVIDE TWO NUMBERS"

    loc 10, 8
    mov str1, input("NUMBER 1 : ")
    mov var1,sval(str1)
    cmp var1,100
    jg mayor1
   
    loc 10, 100
    mov str2, input("NUMBER 2 : ")
    mov var2,sval(str2)
    cmp var2,10
    jg mayor2

; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
;Procedimiento para realizar la división

   
    sub  sval(str1),30H
    sub  sval(str2),30H
    loc  1,19
    print str1
    loc  1,20
    print str2
    push eax
    mov  eax, sval(str1)
    mov  ebx, sval(str2)
    xor  edx,edx     ;prepare for division
    div  ebx
    add  eax,30h      ;convert to an ascii character
    add  ebx,30h
   
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
    mov rslt,sstr$(eax)
    mov resi,sstr$(ebx)
    pop esi

    loc 10, 12
    print "Result = "
    print rslt
    loc 10,14
    print "Residuo = "
    print resi

; ¤=÷=¤=÷=¤=÷=¤=÷=¤=÷=¤=÷=¤=÷=¤=÷=¤=÷=¤=÷=¤=÷=¤=÷=¤=÷=¤=÷=¤=÷=¤=÷=¤=÷=¤=÷=¤
    jmp salida

mayor1:
    cls
    loc 10,12
    mov text, input("EL NUMERO DEBE SER DE UN BIT....")
    mov str1,"   "
    jmp main

mayor2:
    cls
    loc 10,12
    mov text, input("EL NUMERO DEBE SER DE UN BIT....")
    mov str2,"   "
    jmp main

salida:
    loc 10, 20
    mov str1, input("Digite enter para Terminar ....")

    cls

    ret

main endp

; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««

end start

Jeff

what is the problem exactly?  what is making it not work?

the only potential problem i see is that you are not getting the correct remainder ("residuo").  after a 64bit division operation, the remainder is placed in the edx register, not ebx.  that is, if i am understanding the macros and spanish correctly.

OceanJeff32

Hola!

I've seen your program, and you should perhaps break it down into manageable chunks, and then make sure each piece works first, before you put them together.

For example, I see you accept user input (ASCII) then translate to binary.

Make sure that works correctly...

Then you divide the two numbers, while they are in binary.

Make sure that works correctly...

Then you reconvert back to ASCII for display to the user.

Make sure that works correctly...

Little by little, one goes far.

Hope this helps,

Jeff C

P.S. Basically, just don't do too much new stuff at once.
Other than that, I can see you are doing well in understanding Assembly Language, it's not a language for everyone. Computer-ese, if you will.

P.P.S. There is also the BCD (Binary Coded Decimal format) for performing arithmetic operations on ASCII data.  You might want to try that too!

:bg
Any good programmer knows, every large and/or small job, is equally large, to the programmer!

MichaelW

chola,

In addition to the problem that Jeff pointed out,

The sval macro converts a decimal string to a 32-bit integer. Here is the definition:

sval MACRO lpstring       ; string to signed 32 bit integer
  invoke atol, reparg(lpstring)
  EXITM <eax>
ENDM

The macro accepts the address of a string (or a quoted string for which the reparg macro will return the address), calls the MASM32 atol function to convert the string to a 32-bit integer, and returns the result in EAX.

You can convert a single decimal digit to its value by subtracting the character code for '0' (30h), and you can convert a value in the range 0-9 to a decimal digit by adding the character code for '0'. But these simple methods will not work for multi-digit strings or values. If you intend to use the sval macro to do the conversions, you need to eliminate the statements that add and subtract 30h.

In response to your post here:

http://www.masmforum.com/simple/index.php?topic=1295.0

Instead of asking (demanding?) that we take our time to spoon-feed you the basics, why not do what we (or at least most of us) did (and do): read the available references, and if you still don't understand, or doubt that you understand, experiment.

eschew obfuscation

hutch--

I have just deleted 3 postings that broke the rules for the Campus about arguments in the Campus.

Chola needs to learn how to use the DIV and IDIV instructions and if he makes the effort, he will get help, if he does not and persists with dumping his problems in here there is another solution. The "No Homework" rule is not negotiable and it will be enforced so that our members are not subject to abuse.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

chola

;Procedure to do the división of two numbers

; i capture the first number in str1 and the second in str2, the problem is when i run this line invoke atodw, addr str1 the number stored in eax is different that the conversion of this number in hex. Ex. if i enter 4, the conversion in hex is 4, but i check and appears is 18316 and don't know why.

;****************************************************************************************************************

    invoke atodw, addr str1
    push eax
    invoke atodw, addr str2
   
    mov str2,ustr$(eax)
    loc 15, 25
    print str2

    pop ecx
    xor edx, edx
    div ecx
    ;invoke dwtoa,ecx,addr rsltvalue ; convert eax to string
    ;mov rslt, eax
    mov rslt,ustr$(eax)
    mov resi,ustr$(ecx)
   

hutch--

chola,

Try this as an example. Note that using the DIV instruction only does INTEGER divide.


; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
    include \masm32\include\masm32rt.inc
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««

comment * -----------------------------------------------------
                        Build this  template with
                       "CONSOLE ASSEMBLE AND LINK"
        ----------------------------------------------------- *

    .data
      str1  db "5000",0
      str2  db "50",0

    .code

start:
   
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««

    call main
    inkey
    exit

; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««

main proc

    LOCAL num1  :DWORD
    LOCAL num2  :DWORD

    invoke atodw, addr str1     ; convert str1 to a DWORD NUMBER
    mov num1, eax

    invoke atodw, addr str2     ; convert str2 to a DWORD NUMBER
    mov num2, eax

  ; ----------------
    mov eax, num1               ; number to be divided MUST be in EAX
    div num2                    ; divide it
  ; ----------------

    print str$(eax),13,10       ; result is in EAX

    ret


main endp

; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««

end start
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

chola

Thank you to all the people who help me.

:U

:clap: :clap: :clap: :clap: :clap: :clap: :clap: :clap: :clap: :clap: :clap:

THANXXX !!!