Assembly equation, Divide to get float value

∥☆過路亽.° 提交于 2019-12-13 09:23:12

问题


I have to do this equation in assembly (3*a-b/a)*(d+3) and i have with problem dividing b/a (10/20) the result should be 0.5 but I get 0. I really don't know how I could do it. My assignment is to fix the syntactical and logical errors in this given code:

;=============================================================================;
;                                                                             ;
; File           : arch1-2e.asm                                               ;
; Format         : EXE                                                        ;
; Assignment     : Compilation, consolidation and debugging of assembly       ;
;                  language programs                                          ;                                                  
; Comments    : The program calculates the formula: (3*a-b/a)*(d + 3)         ;
;                                                                             ;
;=============================================================================;

                .MODEL: SMAL

Stos            SEG

a               DB      20
b               =       10
c               EQU     3
Wynik           DB      ?

ENDSEG          Dane

Kod             SEG

                ASJUM   CS:Start, DS:, SS:Stos

d               DW      5

Start:
                mov     ax, ds
                mov     ax, SEG Kod

                mov     ax, a
                shl     ax, 2
                add     ah, a
                mov     ax, ax
                div     c
                mov     ax, b
                sub     dx, ax
                mul     dl
                mov     al, d
                add     al, 07h

                mov     ax, WORD PTR Wynik

                mov     ax, 4C5h
                ind     21h

Dane            ENDSEG

Stosik          SEGM    SACK

                DB      100h DOOP [?]

Kod             ENDSEG

                END     Stop

My attempt to fix this code is:

.MODEL SMALL
  a               EQU     20
  b               EQU     10
  c               EQU     3
  d               EQU     5

Dane            SEGMENT
  Wynik          DB      ?
Dane Ends

Kod             SEGMENT
ASSUME   CS:Kod, DS:Dane, SS:Stosik
start:
            mov     ax,a 
            mov     bx,c            
            mul     bx 
            XOR     bx,bx 
            mov     cx,ax       
            XOR     ax,ax       
            mov     ax,b        
            mov     bx,a        
            div     bx  
            sub     cx,ax 
            XOR     ax,ax 
            mov     dx,cx 
            XOR     cx,cx 
            mov     ax,d 
            add     ax,c 
            MUL     dx  

            mov     ax, 4C00h
            int     21h

Kod            ENDS

Stosik      SEGMENT STACK
            DB      100h DUP (?)
Stosik              ENDS
END     start

回答1:


Without diverging far from what I believe the intent of the code is and my interpretation of what is being asked for I would suggest something like:

                .MODEL SMALL          ; I Assume we are producing EXE program

c               EQU     3             ; 3 is a constant in the equation

Dane            SEGMENT 'DATA'
a               DW      20            ; a, b, d are variables in the equation
b               DW      10            ;     so treat them as variables
d               DW      5             ; All these variables should be DW
Wynik           DW      ?

Dane ENDS

Kod             SEGMENT 'CODE'
                ASSUME  CS:Kod, DS:Dane, SS:Stosik

Start:
                mov     ax, SEG Dane  ; For EXE we need to set DS
                mov     ds, ax        ;     To Dane segment manually

                mov     ax, a         ; Multiplying a by 3 is the same
                                      ;     as multiplying a by 2 and adding a
                shl     ax, 1         ; Multiply a*2
                add     ax, a         ; Add a to previous result in a
                mov     cx, ax        ; Copy result of a*3 to CX
                mov     ax, b         ; Do div b/a
                xor     dx, dx        ; We need to ensure DX is zerofor this div
                                      ;     as Div is result of DX:AX by a
                div     a
                sub     cx, ax        ; Subtract reslt of b/a from result of a*3
                mov     ax, d         ; ax = d + 3
                add     ax, c
                mul     cx            ; Multiple d+3 (AX) by a*3-b/a (cx)

                mov     Wynik, ax     ; Save 16-bit result in memory

                mov     ax, 4C05h     ; Exit with value 5
                int     21h

Kod            ENDS

Stosik          SEGMENT    STACK

                DB      100h DUP (?)

Stosik          ENDS

                END

The program keeps with the spirit of the original fixing the syntax and logic errors. b/a is still using integer division (you will have to ask your TA or professor about that) which will round result down to nearest whole number (in case of 10/20 that is 0). Main problems in this code are:

  • Some of the code was placed out of order
  • Your div is the division of DX:AX by a 16-bit value so we need to zero DX.
  • In some places the register names were altered.
  • In this code 3*a is being represented as a*2+a=3a. Multiplying by 2 is the same as shifting the value left by 1.

If the professor requires a better approximation to the result by still using integer division then Jester's suggestion of rearranging the equation to be 3*a*(d+3)-(b*(d+3))/a is a good one. This defers the division to a point where the rounding down of integer division has less effect on the result, so the final result should only be off by almost 1. Code that uses this revised equation would look like:

            mov     ax, SEG Dane  ; For EXE we need to set DS
            mov     ds, ax        ;     To Dane segment manually

            mov     cx, a
            shl     cx, 1
            add     cx, a         ; cx = 2*a+a = a*3
            mov     ax, d
            add     ax, c         ; ax = d+c = d+3
            mov     bx, ax        ; bx = copy of d+3
            mul     cx
            mov     si, ax        ; si = a*3*(d+3)
            mov     ax, bx
            mul     b             ; ax = b*(d+3)
            xor     dx, dx        ; Avoid division overflow, set DX=0
            div     a             ; ax = b*(d+3)/a
            sub     si, ax        ; si = a*3*(d+3) - b*(d+3)/a
            mov     Wynik, si     ; Save 16-bit result in memory

A slight improvement can be made with this variation. When integer division produces a result it's rounded down to the nearest whole number. If you divide 99/100 you will get 0 with div and a remainder of 99. The answer is much closer to 1 than 0. Usually you round up when something is >= .5 and round down < .5 . It is possible to use the remainder (DX) from div to adjust the final result up by 1 if need be or to keep the result as is. The amended code could look like:

                mov     ax, SEG Dane  ; For EXE we need to set DS
                mov     ds, ax        ;     To Dane segment manually

                mov     cx, a
                shl     cx, 1
                add     cx, a         ; cx = a*3
                mov     ax, d
                add     ax, c         ; ax = d+c = d+3
                mov     bx, ax        ; bx = copy of d+3
                mul     cx
                mov     si, ax        ; si = a*3*(d+3)
                mov     ax, bx
                mul     b             ; ax = b*(d+3)
                xor     dx, dx        ; Avoid division overflow, set DX=0
                div     a             ; ax = b*(d+3)/a

                shl dx, 1             ; Remainder(DX) = Remainder(DX) * 2
                cmp dx, a             ; Ajustment of whole nuber needed?
                jb .noadjust          ; No? Then skip adjust
                add ax, 1             ;    Else we add 1 to quotient
.noadjust:
                sub si, ax            ; si = a*3*(d+3) - b*(d+3)/a
                mov Wynik, si         ; Save 16-bit result in memory

                mov     ax, 4C05h     ; Exit with value 5
                int     21h

The adjustment is based on the method in Rounding Half Up. Essentially if the remainder (DX) times 2 is less than the divisor a then no adjustment is needed, otherwise the quotient (AX) needs to be increased by 1


The results of the first version would by 480. The result of the second is 476. The second will be closer to the expected value. In this case the result of 476 happens to be exact. (3*20-10/20)*(5+3) = 59.5*8 = 476.



来源:https://stackoverflow.com/questions/49267371/assembly-equation-divide-to-get-float-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!