16位汇编求数组最大值
时间: 2023-08-25 08:04:25 浏览: 42
以下是 x86 16 位汇编代码,用于在数组中找到最大值:
```
section .data
array dw 5, 3, 8, 2, 7, 1, 9, 4, 6 ; example array
array_len equ $-array ; length of array
section .text
global _start
_start:
; set up registers
mov bx, array ; pointer to array
mov ax, [bx] ; load first element into ax
mov cx, 2 ; counter
find_max:
cmp cx, array_len ; check if we've reached the end of the array
jg end_prog
mov dx, [bx+cx-2] ; load next element into dx
cmp dx, ax ; compare current max with next element
jle not_max ; if next element is not greater, skip update
mov ax, dx ; update max
not_max:
add cx, 2 ; increment counter by 2 (since each element is 2 bytes)
jmp find_max ; repeat for next element
end_prog:
; max value is now in ax
; do something with it here
; exit program
mov ah, 0x4c ; system call for exit
xor al, al ; exit code 0
int 0x21 ; invoke system call
```
这个程序假设数组中的元素都是有符号的 16 位整数。它遍历整个数组,比较每个元素与当前最大值,如果下一个元素大于当前最大值,则更新最大值。最后程序退出,最大值存储在寄存器 AX 中。注意,这个程序使用了 DOS 系统调用来退出程序,因此只能在 DOS 或 Windows 的命令行下运行。如果在 Linux 或其他操作系统中运行,需要修改退出方式。