不难,但麻烦,看在你100分的份上,写一个试试
这个是直接编译成.com文件的,dos下和windows都可以跑,已经试过了没什么问题,注意只针对无符号数,用的是nasm
org 100h
section .data
beg dw 1,3,5,7,9,2,4,6,8,0
end equ $
before db 'before sort:', 13,10,'$'
after db 'after sort:', 13,10,'$'
odd db 'sum of odd: $'
even db 'sum of even: $'
so dw 0
se dw 0
%imacro invoke 1-*
%rep %0-1
%rotate -1
push %1
%endrep
%rotate -1
call %1
%endmacro
%imacro msg 1
mov ah, 9
mov dx, %1
int 21h
%endmacro
section .text
start:
msg before
invoke print, beg, end
invoke qsort, beg, end
msg after
invoke print, beg, end
invoke sum, beg, end
mov [so], ax
mov [se], dx
msg odd
invoke print, so, so+2
msg even
invoke print, se, se+2
mov ax, 4c00h
int 21h
sum:
%define .beg bp+4
%define .end bp+6
enter 0, 0
mov bx, [.beg]
xor ax, ax
xor dx, dx
.L1:
cmp bx, [.end]
jge .L4
test word [bx], 1
jz .L2
add ax, [bx]
jmp .L3
.L2:
add dx, [bx]
.L3:
add bx, 2
jmp .L1
.L4:
leave
ret 4
print:
enter 8, 0
mov byte [bp-1], '$'
mov cx, 10
mov bx, [.beg]
.L1:
cmp bx, [.end]
jge .L6
mov ax, [bx]
xor dx, dx
cmp ax, 0
jne .L2
mov ah, 2
mov dl, '0'
int 21h
jmp .L5
.L2:
mov si, -2
.L3:
cmp ax, 0
je .L4
div cx
or dx, 30h
mov byte [bp+si], dl
dec si
xor dx, dx
jmp .L3
.L4:
inc si
mov ah, 9
lea dx, [bp+si]
int 21h
.L5:
mov ah, 2
mov dl, ' '
int 21h
add bx, 2
jmp .L1
.L6:
mov ah, 2
mov dl, 13
int 21h
mov dl, 10
int 21h
leave
ret 4
qsort:
%define .mid bp-2
enter 2, 0
mov bx, [.beg]
cmp bx, [.end]
jge .L5
mov si, [.end]
sub si, 2
mov cx, [si]
mov di, bx
jmp .L2
.L1:
add bx, 2
.L2:
cmp bx, si
jge .L4
cmp [bx], cx
jae .L3
mov ax, [bx]
xchg ax, [di]
mov [bx], ax
add di, 2
.L3:
jmp .L1
.L4:
xchg cx, [di]
mov [si], cx
mov [.mid], di
invoke qsort, word [.beg], word [.mid]
add word [.mid], 2
invoke qsort, word [.mid], word [.end]
.L5:
leave
ret 4
DSEG SEGMENT
;数据段:在此处添加程序所需的数据
n equ 10
a db 1,3,5,7,9,2,4,6,8,0
evenn db 0
oddn db 0
DSEG ENDS
CSEG SEGMENT
assume cs:CSEG, ds:DSEG
MAIN PROC FAR ;主程序入口
mov ax, dseg
mov ds, ax
;此处添加主程序代码
call sort
call sum
mov bl,evenn
call display
call crlf
mov bl,oddn
call display
next:
;按任意键退出
mov ah,1
int 21h
mov ax, 4c00h ;程序结束,返回到操作系统系统
int 21h
MAIN ENDP
sort proc
mov cx,n
dec cx
loop1: mov di,cx
mov bx,0
loop2: mov al,a[bx]
cmp al,a[bx+1]
jge continue
xchg al,a[bx+1]
mov a[bx],al
continue: add bx,1
loop loop2
mov cx,di
loop loop1
ret
sort endp
sum proc
mov cx,n
lea si,a
next2:
mov al,[si]
test al,01h
jz even1
add oddn,al
jmp next1
even1: add evenn,al
next1: inc si
loop next2
ret
sum endp
display proc
mov ch,10d
call dec_div
mov ch,1d
call dec_div
ret
display endp
dec_div proc near
mov al,bl
mov ah,0
div ch
mov bl,ah
mov dl,al
add dl,30h
mov ah,2
int 21h
ret
dec_div endp
crlf proc near
mov dl,0ah
mov ah,2
int 21h
mov dl,0dh
mov ah,2
int 21h
ret
crlf endp
CSEG ENDS
END MAIN
我也是初学者,所以程序写的不够简练,请见谅,需要解释的话就在这里面留言把,下面是程序
data segment
org 0500h
num db 01h,03h,05h,07h,09h,02h,04h,06h,08h,00h
num1 db 10 dup(?)
data ends
code segment
assume cs:code,ds:data
start:mov cx,10
mov bx,0500h
mov si,0513h
mov al,09h
m0: push bx
m1: cmp [bx],al
jz m1
inc bx
jmp m1
mov [si],al
pop bx
dec si
dec al
jnz m0
mov bx,0509h
m1: inc bx
test [bx],01h
jz m3
add al,[bx]
loop m1
jmp m4
m3:add ah ,[bx]
loop m1
m4:code ends
end start
我没有检查过 最后的奇数结果可以从al寄存器读 偶数可从ah寄存器中读出 这是8086的汇编语言
a