assembly 用玛丽汇编语言计算平均值、求最小值/最大值

jv4diomz  于 2023-06-06  发布在  其他
关注(0)|答案(1)|浏览(124)

我试图找出一种方法来存储用户输入的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
pkmbmrz7

pkmbmrz71#

该解取自OP's question。(已被删除)

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

相关问题