我已编写了一个程序,可以从15个数字的数组中找到最大数字,但输出错误。我将结果存储在AX寄存器中。
这是我的代码。
[org 0x0100]
array_nums: dw 19, 50, 30, 24, 19, 2, 5, 6, 40, 8, 23, 16, 17, 28, 86
max: dw 0
mov bx, 0 ; initialize array index to zero
mov ax, 0 ; initialize min to zero
mov ax, [array_nums+bx] ; max number to ax
mov cx, 15
maxvalue: cmp ax, [array_nums+bx] ; find the maximun number
jge maxloop ; if greater or equal number
mov ax, [array_nums+bx] ; ax contains the maximun number
maxloop:
add bx, 2 ; advance bx to next index
loop maxvalue
mov [max], ax ; write back maximun in memory
mov ax, 0x4c00 ; terminate program
int 0x21
答案 0 :(得分:0)
'我必须将结果存储在......'告诉我这可能是一个功课。
为了取悦你的老师,不要只写一个能解决这个问题的程序。编写一个函数(使其可重用),使用一个API(或者某个标准,或者定义你自己的,比如“数组中的指针必须在BX中定义”。然后将这个函数嵌入到程序中,进行测试。
这样的事情或许(从臀部拍摄,在你使用之前测试它;-))
[org 0x0100]
jmp start
array_nums: dw 19, 50, 30, 24, 19, 2, 5, 6, 40, 8, 23, 16, 17, 28, 86
; ----------------------------------------------------------
; find the largest value in an array of ints
; in-params:
; CX = number of values in array
; BX = pointer to array
; out-params
; AX = biggest value
findBiggest:
mov ax, [bx] ; first number to ax
mov cx, 15
maxvalue: cmp ax, [bx] ; find the maximun number
jge maxloop ; if greater or equal number
mov ax, [bx] ; maximum number was larger
maxloop:
add bx, 2 ; advance bx to next index
loop maxvalue
ret
; ----------------------------------------------------------
start: mov bx, offset array_nums ; use find function on 'array_nums'
mov cx, 15
call findBiggest
; add some code here to show a
; message if AX is what you expected or not
; AX should be 86 for the example array
mov ax, 0x4c00 ; terminate program
int 0x21