2

Trying to teach myself Assembly (masm win32), specifically doing multivariable math with user inputs. I've got the inputs done I believe, but my DOSBox32 will crash after inputs. My guess is it's due to my math section, posted below. Any help would be much appreciated, especially if you can provide insight on how to code a similar equation, as I'm trying to teach myself and I'd rather see patterns and methods over direct answers. Thank you!!

;Math .......   A = ((B * 3) + 6) / (X + D)

mov ax,numB
mov bx,3
imul bx         ;Multiply bx (3) by ax (B)
add ax,6        ;Add 6 to the above
mov bx,numX
add bx,numD     ;Add X + D
idiv bx         ;Divide bx (X + D) by ax ((B * 3) + 6)
mov res,ax      ;Set ax as result
int 21h

;Display Result

mov  ax,@data
mov  ds,ax                       ;set DS to point to the data segment
lea  dx,ResPrompt       ;get ResPrompt 
mov  ah,09h       ;display string function
int  21h                         ;display "A = "
mov  ax,@data
mov  ds,ax                       ;set DS to point to the data segment
lea  dx,res       ;get result 
mov  ah,09h       ;display string function
int  21h                         ;display result

I can post my code before this in case nothing here should cause a crash.

  • Terminology: That's not algebra, you're just evaluating a fixed expression for given numerical values of variables. That's called arithmetic. http://www.mathmedia.com/whatisdifbet.html – Peter Cordes Mar 30 '18 at 18:49
  • http://idownvotedbecau.se/nodebugging/. Where does it crash (which instruction)? Use a debugger to find out. There's no declaration of `numX` and so on, so this isn't a [mcve]. Does execution just fall off the end of the program after printing the results, with no exit system call? – Peter Cordes Mar 30 '18 at 18:51

1 Answers1

2

First reason to fail.

mov res,ax      ;Set ax as result
int 21h

When calling the DOS api, AH needs to hold the function number. You didn't specify one. Depending on the outcome of your calculation, this sequence of instructions can lead to anything!

Second reason to fail.

mov bx,numX
add bx,numD     ;Add X + D
idiv bx

When doing division, you always need to be suspicious about dividing by zero. Depending on the actual contents of the numX and numD variables this could easily be the case.

Third reason to fail.

lea  dx,res       ;get result 
mov  ah,09h       ;display string function
int  21h                         ;display result

This DOS output function expects a pointer to a "$"-terminated text. Your res variable holds a binary value and certainly not a textual representation of the result of the calculation.
For an excellent explanation on displaying numbers see Displaying numbers with DOS.


insight on how to code a similar equation

  • It is important to check for any overflow on mathematical operations. This is especially true when the values in your equation come from user input. See the second reason to fail.

  • Be aware that idiv bx actually divides DX:AX by BX. Your code didn't have to initialize DX because of the multiplication earlier. But what if there weren't such a multiplication in the dividend (any odd equation)?

  • Since the multiplication produced a result in DX:AX, it would have been more correct to add 6 using cascaded addition:

    add ax, 6        ;Add 6 to the above
    adc dx, 0
    

mov  ax,@data
mov  ds,ax

mov  ax,@data
mov  ds,ax

Seeing that you repeatedly setup DS makes me suspicious if you've set it up before the actual calculations.
If you initialize DS early in the program, you're mostly good for the whole program.

Fifoernik
  • 9,779
  • 1
  • 21
  • 27