计算平均值,在Marie汇编语言中查找最小值/最大值

时间:2015-12-17 08:24:14

标签: assembly binary marie

我正在试图找出一种方法来存储用户输入的最小和最多10个数字。我不是在搜索一组数字,而是想在将其添加到其他数字之前比较每个输入数字。我下面的代码将所有输入数字加在一起,然后减去得到平均值,但我也想输出最小值和最大值,我不知道该怎么做。

将输入的数字为:10 20 30 40 50 60 70 80 90 100。

所以目前代码只输出55的平均值

ORG 100

LOOP, LOAD X    /counter
SUBT TEN
SKIPCOND 000   /if X = 10
JUMP LOOP2     /take average after X = 10;

CLEAR           /BEGIN INPUTING NUMBERS FOR AVERAGE
INPUT
ADD Y           /add Y to the input
STORE Y         /save new value
CLEAR
LOAD X         /INCREMENT COUNTER
ADD ONE
STORE X
CLEAR
JUMP LOOP       /RESTART LOOP

LOOP2, LOAD Y    /LOAD ALL THE ADDED NUMBERS
SKIPCOND 800     /WHEN Y = 0
JUMP PRINT       /GO TO OUTPUT

SUBT TEN         /SUBTRACT 10 UNTIL REACH 0
STORE Y          /STORE NEW NUMBER
CLEAR
LOAD AVERAGE     /EACH LOOP ADD 1 TO AVERAGE
ADD ONE
STORE AVERAGE
CLEAR
JUMP LOOP2

PRINT, LOAD AVERAGE
OUTPUT

HALT

TEN, DEC 10
ONE, DEC 1
X, DEC 0          /USE TO INPUT 10 NUMBERS, COUNTER
Y, DEC 0          /ALL NUMBERS INPUT ADDED 
MIN, DEC 0       
MAX, DEC 0
AVERAGE, DEC 0    /AVERAGE OF Y DIVIDED BY X

1 个答案:

答案 0 :(得分:0)

此解决方案来自enter image description here。 (已被删除)

LOOP, LOAD X
    SUBT TEN
    SKIPCOND 000   /if X = 10
    JUMP LOOP2     /take average after X = 10;

    CLEAR           /BEGIN INPUTING NUMBERS FOR AVERAGE
    INPUT
    STORE TEMP
    CLEAR

IF, LOAD X
    SUBT 1
    SKIPCOND 800
    JUMP STOREFIRST

    CLEAR
    LOAD TEMP
    SUBT MIN
    SKIPCOND 000
    JUMP FINDMAX

ELSE, SKIPCOND 800
    JUMP FINDMIN

    CONTINUE, CLEAR
    LOAD TEMP
    ADD Y           /add Y to the input
    STORE Y         /save new value
    CLEAR
    LOAD X         /INCREMENT COUNTER
    ADD ONE
    STORE X
    CLEAR
    JUMP LOOP       /RESTART LOOP

    FINDMAX, CLEAR
    LOAD TEMP
    STORE MAX
    JUMP CONTINUE

    FINDMIN, CLEAR
    LOAD TEMP
    STORE MIN
    JUMP CONTINUE

STOREFIRST, LOAD TEMP
    STORE MIN
    STORE MAX
    JUMP CONTINUE

LOOP2, LOAD Y    /LOAD ALL THE ADDED NUMBERS
    SKIPCOND 800     /WHEN Y = 0
    JUMP PRINT       /GO TO OUTPUT

    SUBT TEN         /SUBTRACT 10 UNTIL REACH 0
    STORE Y          /STORE NEW NUMBER
    CLEAR
    LOAD AVERAGE     /EACH LOOP ADD 1 TO AVERAGE
    ADD ONE
    STORE AVERAGE
    CLEAR
    JUMP LOOP2

    PRINT, LOAD AVERAGE
    OUTPUT
    CLEAR
    LOAD MIN
    OUTPUT
    CLEAR
    LOAD MAX
    OUTPUT
    CLEAR

    HALT

TEN, DEC 10
ONE, DEC 1
X, DEC 0          /USE TO INPUT 10 NUMBERS, COUNTER
Y, DEC 0          /ALL NUMBERS INPUT ADDED 
MIN, DEC 0       
MAX, DEC 0
TEMP, DEC 0
AVERAGE, DEC 0    /AVERAGE OF Y DIVIDED BY X