以前我们的程序都没涉及负数 这次做一个排序的程序 且能处理负数。
看C代码及效果图:
我们用汇编来实现 这里的关键是判断负数和显示负数:
程序代码:
[ 本帖最后由 有容就大 于 2012-10-11 15:10 编辑 ]
看C代码及效果图:
我们用汇编来实现 这里的关键是判断负数和显示负数:
程序代码:;#Mode=DOS
;MASMPlus
;--------------------------------------------------------------------
;--------------------------------------------------------------------
; program name: Sort
; producer: yrjd
; program function: Sort a series of numbers and show it
; produce data: 2012-10-11 Thursday
;--------------------------------------------------------------------
assume cs:code, ds:data, ss:stack
stack segment para stack 'stack'
db 30 dup (?)
stack ends
data segment
NumArray dw 56, 57, 4767, 4, 543, 76, 44, 0fffch, 0eefch, 0
Index dw ?
NumString db 128 dup('$')
rPrompt db 'The sum is : ', '$'
EndPrompt db 'Press any key to continue', '$'
data ends
code segment
start: ; Segment register initialize 段寄存器初始化
mov ax, stack
mov ss, ax
mov sp, 30
mov ax, data
mov ds, ax
; Sort 排序
mov cx, 9
mov si, 0
Sort: mov bx, NumArray[si]
mov di, si
mov Index, si
add di, 2
InSort: cmp di, 18
ja GetSingle
cmp bx, NumArray[di]
jng Ctnu
mov bx, NumArray[di]
mov ax, di
mov Index, ax
Ctnu: add di, 2
jmp InSort
GetSingle: push di
mov di, Index
push NumArray[si]
push NumArray[di]
pop NumArray[si]
pop NumArray[di]
pop di
push NumArray[si]
add si, 2
loop Sort
push NumArray[si]
mov cx, 10
mov di, 0
StoreString: mov dx, 0
pop ax
call dis
loop StoreString
; Prompt and Output the result 提示并输出结果
lea dx, rPrompt
mov ah, 09h
int 21h
lea dx, NumString
mov ah, 09h
int 21h
call crlf
; Output End Prompt 输出结束提示
lea dx, EndPrompt
mov ah, 09h
int 21h
; View the result and Return DOS 查看结果并返回DOS
mov ah, 01h
int 21h
mov ah, 4ch
int 21h
dis:
push bx
push cx
push dx
push si
push ax
; Judge negative 判断是不是负数
mov bx, ax
cmp ax, 0
jnl Positive
mov cx, 0ffffh
sub cx, ax
inc cx
mov ax, cx
; Get each bit digit 分解结果的各个位的数字
Positive: mov si, 0
GetBit: mov cx, 10
call divdw
add cx, 30h
push cx
inc si
cmp ax, 0
jz CmpAgain
jmp GetBit
CmpAgain: cmp dx, 0
jz GetEnd
jmp GetBit
GetEnd: cmp bx, 0
jnl Positive1
mov bl, '-'
mov bh, 0
push bx
inc si
Positive1: mov cx, si
GetResult: pop ax
mov NumString[di], al
inc di
loop GetResult
mov al, ' '
mov NumString[di], al
inc di
pop ax
pop si
pop dx
pop cx
pop bx
ret
; The function of Carriage-Return Line-Feed 回车换行
crlf: mov dl, 0dh
mov ah, 02h
int 21h
mov dl, 0ah
mov ah, 02h
int 21h
ret
divdw: ; not overflow division 实施不溢出除法获取除10的余数
push si
push di
push ax
mov ax, dx
mov dx, 0
div cx
mov si, ax
pop ax
div cx
mov cx, dx
mov dx, si
pop di
pop si
ret
code ends
end start[ 本帖最后由 有容就大 于 2012-10-11 15:10 编辑 ]

梅尚程荀
马谭杨奚






