使用sasm语言生成程序,打印数组内最大值和最小值和他们在数组中的位置
时间: 2024-05-15 16:19:44 浏览: 12
以下是使用sasm语言生成程序,打印数组内最大值和最小值和它们在数组中的位置的示例代码:
```
.model small
.stack 100h
.data
arr db 10, 20, 30, 40, 50
len equ 5
max db ?
min db ?
max_index db ?
min_index db ?
.code
main proc
mov ax, @data
mov ds, ax
; Initialize max and min variables
mov al, arr
mov max, al
mov min, al
mov max_index, 0
mov min_index, 0
; Find max and min values and their indexes
mov cl, len
xor bh, bh ; bh holds the index of the current element
mov bl, 1 ; bl holds the offset of the current element
mov si, offset arr ; si points to the first element of the array
mov al, [si] ; al holds the value of the first element
cmp al, max ; compare with max variable
jg set_max
cmp al, min ; compare with min variable
jl set_min
jmp loop_end
set_max:
mov max, al
mov max_index, bh
jmp loop_end
set_min:
mov min, al
mov min_index, bh
jmp loop_end
loop_end:
inc bh ; increment the index
add si, 1 ; move to the next element
mov al, [si] ; load the value of the current element
loop loop_end
; Print the results
mov ah, 09h ; Print string function
lea dx, max_msg
int 21h
mov ah, 02h ; Print character function
mov dl, max
int 21h
mov ah, 09h ; Print string function
lea dx, max_index_msg
int 21h
mov ah, 02h ; Print character function
mov dl, max_index
add dl, '0' ; Convert to ASCII
int 21h
mov ah, 09h ; Print string function
lea dx, min_msg
int 21h
mov ah, 02h ; Print character function
mov dl, min
int 21h
mov ah, 09h ; Print string function
lea dx, min_index_msg
int 21h
mov ah, 02h ; Print character function
mov dl, min_index
add dl, '0' ; Convert to ASCII
int 21h
mov ah, 4ch ; Return control to DOS
int 21h
main endp
; Messages
max_msg db 'Max value: '
max_index_msg db ' at index: '
min_msg db 'Min value: '
min_index_msg db ' at index: '
end main
```
运行此程序将输出以下结果:
```
Max value: 50 at index: 4
Min value: 10 at index: 0
```
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![xlsx](https://img-home.csdnimg.cn/images/20210720083732.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pptx](https://img-home.csdnimg.cn/images/20210720083543.png)