assembly 我怎样才能让这个玛丽程序对底片起作用?

busg9geu  于 2023-10-19  发布在  其他
关注(0)|答案(2)|浏览(103)

我写了这个程序,将两个数字相乘在一起,但现在我需要使它工作的负数,任何人都有任何建议?

org 100
Input a /first factor
Store a /store first factor
Input b /second factor
Store b /store second factor
Load a /load first factor
Skipcond 800 /checks if number is greater than zero
Jump End /skips to the end if zero
load b /load second factor
Skipcond 800 /checks if number is greater than zero
Jump End /skips to the end if zero
Loop, Load b /reloads second factor
Subt One /subtracts one from the second factor
Store b /store the new value of the second factor
Load productAB /load the product of the two number; initially zero
Add a /add first factor to the product
Store productAB /store new value to product
Load b/ load second factor again
Skipcond 400 /end loop if b is equal to 0
Jump Loop /repeats the loop
Load productAB /load the value of productAB; no longer 0
End, output /print out results
Halt /end of program

a, Dec 0
b, Dec 0
productAB, Dec 0 /product of the first two numbers
One, Dec 1
vawmfj5a

vawmfj5a1#

使用伪代码

neg = 0
if (a < 0)
    a = -a
    neg = 1
if (b < 0)
    b = -b
    neg ^= 1
productAB = a * b
if (neg)
    productAB = -productAB
dced5bon

dced5bon2#

下面是适用于所有整数的乘法算法的优化版本的伪代码:

product = 0
    is_negative = False

    if a < 0 and b > 0 or a > 0 and b < 0:
        is_negative = True

    # make both a and b positive
    if a < 0:
        a = -a
    if b < 0:
        b = -b

    # a should be the bigger number
    if a < b:
        temp = a
        a = b
        b = temp

    # perform multiplication
    for i in range(0, b):
        product += a

    if is_negative:
        return 0-product
    return product

通过实现交换功能,代码在将一个大数字和一个小数字相乘时需要更少的迭代。例如,当乘以1000*2时,修改后的算法只做1000+1000而不是2+2+...+2
下面是相应的MARIE.js代码:

input
store a

input
store b

jns check_sign
jns make_a_pos
jns make_b_pos
jns mult

/ check true sign of product
load negative
Skipcond 400
jump output_negative_product
/ Output  positive product
load product
output
halt
 
check_sign, hex 0
            load a
            Skipcond 000
            jump check_sign_given_a_pos/ do if a >= 0
            
            / if a < 0
            load b 
            Skipcond 000
            jump set_neg_true / a < 0 and b > 0
            JumpI check_sign / a < 0 and b < 0

check_sign_given_a_pos,     load b 
                            Skipcond 000
                            JumpI check_sign / a >= 0 and b >= 0
                            jump set_neg_true / a >= 0 and b < 0

/ negative = 1 and returns to main
set_neg_true,   load one
                store negative
                JumpI check_sign

/ a =  abs(a)
make_a_pos,     hex 0
                load a
                Skipcond 000
                jumpi make_a_pos
                load zero
                subt a
                store a
                jumpi make_a_pos
                
/ b = abs(b)
make_b_pos,     hex 0
                load b
                skipcond 000
                jumpi make_b_pos
                load zero
                subt b
                store b
                jumpi make_b_pos
 
output_negative_product,    load zero
                            Subt  product
                            store product
                            output
                            halt
                    

/ performs product = abs(a) * abs(b), where abs(a) >= abs(b)            
mult,   hex 0
        load a
        subt b
        
        / a < b ?
        skipcond 000
        Jump mult_loop
        
        / swap a and b
        load a
        store temp
        
        load b
        store a
        
        load temp
        store b
        
mult_loop,  load i
            subt b
            / i < b ?
            skipcond 000
            JumpI mult

            load product
            add a / product += a
            store product

            / i++
            load i
            add one
            store i

            jump mult_loop

negative, dec 0 / initially false. 
one, dec 1 
zero, dec 0
i, dec 0
temp, dec 0
a, dec 0
b, dec 0
product, dec 0

相关问题