装配方程式,除以得到浮点值 [英] Assembly equation, Divide to get float value

查看:62
本文介绍了装配方程式,除以得到浮点值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须在装配体(3*a-b/a)*(d+3)中执行此方程,并且我必须解决b/a(10/20)的问题,结果应为0.5,但我得到0.我真的不知道该怎么做.我的任务是修复此给定代码中的语法和逻辑错误:

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

推荐答案

与我认为代码的意图和我对所要查询内容的解释没有什么不同,我建议如下:

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

该程序保持了原来的精神,修复了语法和逻辑错误. b/a仍在使用整数除法(您将不得不询问助教或教授),这会将结果四舍五入到最接近的整数(在10/20为0的情况下).此代码中的主要问题是:

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:

  • 某些代码顺序混乱
  • 您的div是DX:AX除以16位值,因此我们需要将DX归零.
  • 在某些地方,寄存器名称已更改.
  • 在此代码中3 * a被表示为a * 2 + a = 3a.乘以2等于将值左移1.
  • 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.

如果教授仍通过使用整数除法要求更好地近似结果,则杰斯特关于将方程重新排列为3 * a *(d + 3)-(b *(d + 3))/a的建议是好一个.这将除法延缓到整数除法对结果的影响较小的程度,因此最终结果应仅差近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

使用此变体可以略微改善.当整数除法产生结果时,会将其四舍五入为最接近的整数.如果将99/100除以,则div将得到0,而余数为99.答案比1更接近于1,而不是0.通常当> = .5时向上舍入,而<则向下舍入. .5.如果需要,可以使用div中的余数(DX)将最终结果上调1,或者保持结果不变.修改后的代码可能类似于:

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

调整基于舍入一半中的方法.本质上,如果剩余(DX)乘以2小于除数a,则无需进行调整,否则商(AX)需要增加1

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

第一个版本的结果为480.第二个版本的结果为476.第二个版本的结果更接近期望值.在这种情况下,476的结果恰好是准确的. (3 * 20-10/20)*(5 + 3)= 59.5 * 8 = 476.

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.

这篇关于装配方程式,除以得到浮点值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆