English 中文(简体)
此 MARIE 代码是否找到数组中最低的数字?
原标题:Does this MARIE code find the lowest number in an array?
我试图写入一个程序, 找到一个内存中的数组的最小值 。 这是我的代码 : Org 100 / find 最小的装入启动 添加 Num Subt 一个存储器, loc Loc LocStorre Min 装载 Num Subt 一个存储器 J Loop, 加载 J Skippond 00 跳跃 跳跃完成后, 装入 Loc Subt 一个存储器 2, 加载 J Subt 1 Store J 跳跳圈完成, 装入 Min Halp Min, Dec 0 / 在哪里我们保存分钟 Num, Dec 10 / 数值, 显示在一阵列中保存 Dec 1 J, Dec 0/ Loop 变量 Loc, 开始 Dec 0, 启动, Hex 121 / 确定在阵列中启动值, 12月 5 20 Dec 15 Dec 83 12 12 7 12 13 15 我尝试运行它, 但它却不正确确定 1 为最小值 。 。 但是它会得到 15 ( )
问题回答
This looks suspicious: Loadi Loc Store Min What do you have at zero address? [edit] Ah that was right, but another thing: doesn t it skip the 9th array element? I understand it initializes by : storing the 10th element as (current) min, sets ptr to 9th element and sets loop count to 9. Then the first thing in the loop - after checking the loop counter, it decrements the pointer again before checking the "next" element. Or did I understand something wrong? [edit #2] Ahah, the Marie behind the link I gave didn t understand the indirect instructions. This does. http://computerscience.jbpub.com/ecoa/3e/simulators.aspx It looks like there is something else wrong too: This doesn t make sense: After, Load Loc Subt One Store Loc Subt Min / here Min is subtracted from the element s address Skipcond 00 I guess the program is supposed to find the minimum, but it doesn t work at all.
There are two issues: The sequence Store Loc followed by Subt Min makes no sense as Loc is an address and Min is an array value. Subtracting those to make a comparison is not helpful: it is like comparing apples with pears. What you intended to happen is that the value at Loc would be read into the accumulator and that value would be compared with Min. To make that happen you need to insert Loadi Loc between those two instructions. J represents the number of array values still to visit, but when it becomes 0, you still make one more iteration of the loop. This means a value will be read that is not part of the array, which could potentially be less than the minimum value of the array (not in this example) and lead to a wrong result. The extra iteration is made because Skipcond 00 will skip when the accumulator is less than zero, not yet when it is zero. There are several ways to fix this bug, for instance by replacing this instruction with Slipcond 400 which will skip when the accumulator is zero. That gives the desired result. With those two fixes it will work. Some other comments on the code: Start, Hex 121 /Be sure to start off values in array : this comment highlights the potential problem: if ever you modify the program, this address may change. It is a pity you have to calculate it manually. MARIE has the ADR directive which you can use here: Start, ADR Array It would be practical if the program would output the result, so add an Output instruction just before Halt. The name of the variable J is not very descriptive. CountDown would be more descriptive. The directive Org 100 is not really needed; it is just fine if your program is loaded from memory location 0 onwards. Drop it. You could do the loop without the counter (J) and instead compare the current location with the start address of the array. Revised program Load ArrayAdr Add ArraySize Subt One Store CurrentAdr / Refers to last array value UpdateMin, Loadi CurrentAdr Store Minimum Loop, Load CurrentAdr Subt ArrayAdr Skipcond 800 / Skip when current address greater than start of array Jump Done / Otherwise we are done Load CurrentAdr Subt One Store CurrentAdr LoadI CurrentAdr / Compare current value with Minimum Subt Minimum Skipcond 000 / Skip when array value < Min Jump Loop / Otherwise loop without updating Minimum Jump UpdateMin Done, Load Minimum Output Halt / Constants One, Dec 1 ArraySize, Dec 10 / Number of values in array ArrayAdr, Adr Array / Address of Array Array, Dec 800 Dec 600 Dec 810 Dec 650 Dec 830 Dec 650 Dec 700 Dec 522 Dec 530 Dec 450 / Variables Minimum, Dec 0 / The minimum value in the array so far CurrentAdr, Dec 0 / The address of the current array value You can assemble and run this program on MARIE.js.org.




相关问题
WordPress Data Storage Efficiency

I ve been asked to review a WordPress plugin of sorts and try to find ways of making it faster. The premise of this plugin is basically to store a bunch of users and shifts and appointments and ...

Convert a 2D array index into a 1D index

I have two arrays for a chess variant I am coding in java...I have a console version so far which represents the board as a 1D array (size is 32) but I am working on making a GUI for it and I want it ...

Convert an array of integers for use in a SQL "IN" clause

Surely there is a framework method that given an array of integers, strings etc converts them into a list that can be used in a SQL "IN" clause? e.g. int[] values = {1,2,3}; would go to "(1,2,3)"

Sorting twodimensional Array in AS3

So, i have a two-dimensional Array of ID s and vote count - voteArray[i][0] = ID, voteArray[i][1] = vote count I want the top 3 voted items to be displayed in different colors, so i have a 2nd Array -...

C++ Array Sort Me

Stuck on an array sorter. Have to sort numbers from largest to smallest. I m trying two loops (one nested in the other). Here s the code: int counter=0; // inner counter int counter2=0; // outer ...

PHP array callback functions for cleaning output

I have an array of output from a database. I am wondering what the cleanest way to filter the values is example array Array ( [0] => Array ( [title] => title 1 ...

Best practice of big javascript objects

sry for this imprecise topic name. I am querying a dataset a lot of times so using ajax request would end up in tons of http requests. For this reason I decided to use the json encode method to ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

热门标签