Finding the largest number in an array using assembly code?
So I'm trying to figure out how to write a program that will find the largest number in the array but it needs to use assembly meaning that I have a limited set of commands (ie add, subtract, jump if 0 and jump if less then zero). Here is what I have so far:
C00A 000 jmp start skip over the variables
0001 001 dw a[0]
0002 002 dw a[1]
0003 003 dw a[2]
0004 004 dw a[3]
0005 005 dw a[4]
0006 006 dw a[5]
0000 007 dw
0000 008 leave space for changes
0000 009
1000 00A start so now what?
I have the array initialized with a couple of hard coded values and now I need to write a type of loop that will find the largest one among them. This is what I have figured out so far:
load a[0] load first value
sub a[1] subtract second value
jmpl the result is negative (meaning a[1]>a[0]) jump somewhere
I understand the underlying philosophy but I just don't know how to create a system that will "loop" until it find the max value.
This is basically what you need to code in assembly language.
load a[0] ;load first value
sub a[1] ;subtract second value
jmpl IsBigger1
load a[0] ;reload first value
store a[1] ;replace second value
IsBigger1:
load a[1] ;load second value
sub a[2] ;subtract third value
jmpl IsBigger2
load a[1] ;reload second value
store a[2] ;replace third value
IsBigger2:
I've shown 2 blocks of code. You should add 3 such blocks and then you'll find the max value in the last element of the array a[5].
This is an unrolled version of the loop that you need. Because you didn't specify the architecture I can't possibly suggest the necessary instructions to turn this into a real loop. Your "limited set of commands" doesn't even mention load
.
EDIT
It's a pitty this architecture forces us to write self modifying code.
Nevertheless it was great fun writing my first IBCM program!
addr dw 1
count dw 5
one dw 1
isload dw 3000
isstore dw 4000
issub dw 6000
again load count
sub one
jmpl exit
store count
load isload
add addr
store a
load issub
add addr
add one
store b
a nop
b nop
jmpl bigger
load isload
add addr
store c
load isstore
add addr
add one
store d
c nop
d nop
bigger load addr
add one
store addr
jmp again
exit halt
链接地址: http://www.djcxy.com/p/60384.html
上一篇: 我如何嵌入jwplayer,使其在Facebook流中内联播放?
下一篇: 使用汇编代码找到数组中最大的数字?