以下是引用zklhp在2012-10-6 19:42:06的发言:
还有 其实这里主要是DOS的可视化 而非汇编的可视化。。。
还有 其实这里主要是DOS的可视化 而非汇编的可视化。。。
恩 16位机范围比较窄 WIN32汇编才稍微接触点 不敢瞎写啦 希望别的坛友发点帖子说说这方面的心得啊。

梅尚程荀
马谭杨奚


程序代码:#include <stdio.h>
int main(void)
{
int a = 34724354, b = 19568435, sum;
sum = a + b;
printf("The sum is %d \n", sum);
return 0;
}--
程序代码:;#Mode=DOS
;MASMPlus
;--------------------------------------------------------------------
;--------------------------------------------------------------------
; program name: AddTwoNumber
; producer: yrjd
; program function: Calculate the sum of two number and show it
; produce data: 2012-10-6 Saturday
;--------------------------------------------------------------------
assume cs:code, ds:data, ss:stack
stack segment
db 128 dup(?)
stack ends
data segment
aNumber dd 34724354
bNumber dd 19568435
rNumber db 20 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, 128
mov ax, data
mov ds, ax
; deal with the add procedure 处理加法过程
mov ax, WORD ptr aNumber[0]
add ax, WORD ptr bNumber[0]
mov dx, WORD ptr aNumber[2]
adc dx, WORD ptr bNumber[2]
; Get each bit digit 分解结果的各个位的数字
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: mov cx, si
mov di, 0
GetResult: pop ax
mov rNumber[di], al
inc di
loop GetResult
; Prompt and Output the result 提示并输出结果
lea dx, rPrompt
mov ah, 09h
int 21h
lea dx, rNumber
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
; 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--
程序代码:;#Mode=DOS
;MASMPlus
;--------------------------------------------------------------------
;--------------------------------------------------------------------
; program name: MulTwoNumber
; producer: yrjd
; program function: Calculate the Product of two number and show it
; produce data: 2012-10-7 Sunday
;--------------------------------------------------------------------
assume cs:code, ds:data, ss:stack
stack segment
db 128 dup(?)
stack ends
data segment
aNumber dw 14533
bNumber dw 34530
rNumber dw 20 dup('$')
rPrompt db 'The result is : ', '$'
EndPrompt db 'Press any key to continue', '$'
data ends
code segment
start: ; Segment register initialize 段寄存器初始化
mov ax, stack
mov ss, ax
mov sp, 128
mov ax, data
mov ds, ax
; deal with the add procedure 处理加法过程
; 两个数相乘 可以拆分成加法 也可以直接相乘
;-----------------------------------------------------------------
; 此为分解成加法的过程
; mov cx, aNumber
; mov ax, 0
; mov dx, 0
; AddLoop: add ax, WORD ptr bNumber[0]
; adc dx, 0
; loop AddLoop
;------------------------------------------------------------------
;------------------------------------------------------------------
; 此为直接相乘
mov ax, aNumber
mov dx, 0
mov cx, bNumber
mul cx
;------------------------------------------------------------------
; Get each Decimal digit 分解结果的各个位的数字
mov si, 0
GetDec: mov cx, 10
call divdw
add cx, 30h
push cx
inc si
cmp ax, 0
jz CmpAgain
jmp GetDec
CmpAgain: cmp dx, 0
jz GetEnd
jmp GetDec
GetEnd: mov cx, si
mov di, 0
GetResult: pop ax
mov BYTE ptr rNumber[di], al
inc di
loop GetResult
; Prompt and Output the result 提示并输出结果
lea dx, rPrompt
mov ah, 09h
int 21h
lea dx, rNumber
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
; 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---
程序代码:;#Mode=DOS
;MASMPlus 单文件代码模板 - 纯 DOS 程序
;--------------------------------------------------------------------
;--------------------------------------------------------------------
; program name: AddTwoNum
; producer: yrjd
; program function: Calculate the sum of two number and show it
; produce data: 2012-10-7 Sunday
;--------------------------------------------------------------------
assume cs:code, ds:data, ss:stack
stack segment
db 128 dup (?)
stack ends
data segment
NumberA db 80
LenA db ?
ActA db 80 dup (0)
NumberB db 80
LenB db ?
ActB db 80 dup (0)
NumberR dw 0, 0, 0, 0, 0, 0
StringR db 80 dup ('$')
PromptA db 'Please input Number a: ', '$'
PromptB db 'Please input Number b: ', '$'
PromptR db 'The sum of a and b is: ', '$'
PromptEnd db 'Press any key to continue', '$'
data ends
code segment
START: ;; Segment register initialize 段寄存器初始化
mov ax, stack
mov ss, ax
mov sp, 128
mov ax, data
mov ds, ax
;; Prompt input number a 提示输入a
lea dx, PromptA
mov ah, 09h
int 21h
;; Input a and set into NumberA 输入a并放置在NumberA中
lea dx, NumberA
mov ah, 0ah
int 21h
call CRLF
;; Convert the string to decimal number 把数字字符串转换为十进制数
mov cl, LenA
mov ch, 0
mov ax, 0
mov dx, 0
lea si, ActA
add si, cx
dec si
mov di, 0
ConvertA: push cx
sub [si], 30h
mov al, BYTE ptr [si]
mov ah, 0
mov dx, 0
cmp di, 0
jz SingleA
mov bx, 10
mov cx, di
MulTenA: mul bx
loop MulTenA
SingleA: add NumberR[0], ax
adc NumberR[2], dx
dec si
pop cx
inc di
loop ConvertA
;; Prompt input number b 提示输入b
lea dx, PromptB
mov ah, 09h
int 21h
;; Input a and set into NumberB 输入a并放置在NumberB中
lea dx, NumberB
mov ah, 0ah
int 21h
call CRLF
;; Convert the string to decimal number 把数字字符串转换为十进制数
mov cl, LenB
mov ch, 0
mov ax, 0
mov dx, 0
lea si, ActB
add si, cx
dec si
mov di, 0
ConvertB: push cx
sub [si], 30h
mov al, BYTE ptr [si]
mov ah, 0
mov dx, 0
cmp di, 0
jz SingleB
mov bx, 10
mov cx, di
MulTenB: mul bx
loop MulTenB
SingleB: add NumberR[4], ax
adc NumberR[6], dx
dec si
pop cx
inc di
loop ConvertB
; Add the two Converted Decimal number
; 把转换的两个十进制数加起来放在ax, dx中
mov ax, NumberR[0]
add ax, NumberR[4]
mov dx, NumberR[2]
adc dx, NumberR[6]
; Get each point number 分解结果的各个位的数字
mov si, 0
GetEachNum: mov cx, 10
call divdw
add cx, 30h
push cx
inc si
cmp ax, 0
jz CmpAgain
jmp GetEachNum
CmpAgain: cmp dx, 0
jz GetEnd
jmp GetEachNum
GetEnd: mov cx, si
mov di, 0
GetResult: pop ax
mov StringR[di], al
inc di
loop GetResult
;; Prompt the result's output 提示并输出结果
lea dx, PromptR
mov ah, 09h
int 21h
lea dx, StringR
mov ah, 09h
int 21h
call CRLF
; Output End Prompt 输出结束提示
lea dx, PromptEnd
mov ah, 09h
int 21h
; View the result and Return DOS 查看结果并返回DOS
mov ah, 01h
int 21h
mov ah, 4ch
int 21h
;; The function of Carriage-Return Line-Feed 回车换行函数
CRLF: push ax
push dx
mov dl, 0dh
mov ah, 02h
int 21h
mov dl, 0ah
mov ah, 02h
int 21h
pop dx
pop ax
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这个方法的大概构思就是将两个字符串转换为两个十进制数后相加 在显示出来
程序代码:;#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
程序代码:;#Mode=DOS
;MASMPlus
;--------------------------------------------------------------------
;--------------------------------------------------------------------
; program name: Search
; producer: yrjd
; program function: Search a number in an array
; produce data: 2012-10-12 Friday
;--------------------------------------------------------------------
assume cs:code, ds:data, ss:stack
stack segment para stack 'stack'
db 128 dup (?)
stack ends
data segment
NumArray dw 0ffd3h, 75, 9877, 43, 0fffdh, 0, 653, 776, 41a1h, 666
SearchNum db 32
NumLen db ?
ActNum db 32 dup (?)
InputPrompt db 'Please input the Number you want to search: ', '$'
NotFound db 'Can not find the number!', '$'
HasFound db 'It has found the number!', '$'
EndPrompt db 'Press any key to continue', '$'
data ends
code segment
start: ; Segment register initialize 段寄存器初始化
mov ax, stack
mov ss, ax
mov sp, 128
mov ax, data
mov ds, ax
; Prompt and Input the search number 提示并输入要查找的数
lea dx, InputPrompt
mov ah, 09h
int 21h
lea dx, SearchNum
mov ah, 0ah
int 21h
call ConvertToHex
; Compare and show the result
; 比较并输出结果
mov cx, 10
mov si, 0
mov al, 0
SearchFor: cmp di, NumArray[si]
jz AddCount
next: add si, 2
jmp s
AddCount: inc al
add si, 2
s: loop SearchFor
call crlf
cmp al, 0
jz CannotFound
lea dx, HasFound
mov ah, 09h
int 21h
jmp OK
CannotFound: lea dx, NotFound
mov ah, 09h
int 21h
; Output End Prompt 输出结束提示
OK: call crlf
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
; The function of Carriage-Return Line-Feed 回车换行
crlf: push ax
push dx
mov dl, 0dh
mov ah, 02h
int 21h
mov dl, 0ah
mov ah, 02h
int 21h
pop dx
pop ax
ret
; Convert the string to a Hex number 把输入的字符串转化为十六进制数
ConvertToHex: push ax
push bx
push cx
push dx
push si
mov cl, NumLen
mov ch, 0
mov si, 0
cmp BYTE ptr ActNum[si], '-'
jnz PushBCD
dec cl
mov si, 1
PushBCD: mov dl, BYTE ptr ActNum[si]
sub dl, 30h
mov dh, 0
push dx
inc si
loop PushBCD
mov cl, NumLen
mov ch, 0
mov bx, 10
mov si, 0
mov di, 0
cmp BYTE ptr ActNum[0], '-'
jnz GetHex
dec cl
GetHex: pop ax
push cx
cmp si, 0
jz AddEach
mov cx, si
MulTen: mov dx, 0
mul bx
loop MulTen
AddEach: add di, ax
inc si
pop cx
loop GetHex
cmp BYTE ptr ActNum[0], '-'
jnz ConvertOK
mov ax, 65535
sub ax, di
inc ax
mov di, ax
ConvertOK: pop si
pop dx
pop cx
pop bx
pop ax
ret
code ends
end start

程序代码:;#Mode=DOS
assume cs:code, ds:data, ss:stack
stack segment para 'stack'
db 128 dup (?)
stack ends
data segment
EndPrompt db 'Press any key to continue...', '$'
InputPrompt db 'Please input the year:', '$'
IsLeapYear db 'This is a leap year!', '$'
NotLeapYear db 'This is not a leap year!', '$'
AskContinue db 'Do you want to continue?(Y/N):', '$'
YearString db 10
YSLen db ?
YSAct db 10 dup ('$')
YearValue dw 0
data ends
code segment
start: ;; Initialize the segment register 段寄存器初始化
mov ax, stack
mov ss, ax
mov sp, 128
mov ax, data
mov ds, ax
;; Prompt user to input a year 提示用户输入一个年份
InputYear: mov dx, offset InputPrompt
mov ah, 09h
int 21h
;; Input a year as string 以字符串形式输入年份
lea dx, YearString
mov ah, 0ah
int 21h
call CheckYear
call crlf
cmp ax, 0
je InputYear
mov YearValue[0], 0
call ConvertStr
call JudgeLeap
call crlf
;; Ask the user whether to continue
;; 询问是否继续
lea dx, AskContinue
mov ah, 09h
int 21h
;; Input the choice
;; 输入你的选择
mov ah, 01h
int 21h
call crlf
cmp al, 'Y'
je InputYear
;; Prompt end and return 程序结束并返回
mov dx, offset EndPrompt
mov ah, 09h
int 21h
mov ah, 01h
int 21h
mov ah, 4ch
int 21h
crlf: ;; Carriage-Return Line-Feed 回车换行
push dx
push ax
mov dl, 0dh
mov ah, 02h
int 21h
mov dl, 0ah
mov ah, 02h
int 21h
pop ax
pop dx
ret
;; Check the legitimacy of the input (inside four number characters)
;; 检查输入的合法性(在四个数字字符内)
CheckYear: push cx
push si
cmp YSLen, 4
ja InputError
mov cl, YSLen
mov ch, 0
mov si, 0
CheckNum: cmp byte ptr YSAct[si], '0'
jb InputError
cmp byte ptr YSAct[si], '9'
ja InputError
inc si
loop CheckNum
mov ax, 1
jmp CheckEnd
InputError: mov ax, 0
CheckEnd: pop si
pop cx
ret
;; COnvert the Year's string to Year's Value
;; 把年份的字符串转换为数值
ConvertStr: push ax
push bx
push cx
push dx
push si
push di
mov cl, YSLen
mov ch, 0
mov si, cx
dec si
mov di, 0
mov bx, 10
DealEach: push cx
mov al, BYTE ptr YSAct[si]
sub al, 30h
mov ah, 0
mov dx, 0
cmp di, 0
je AddEach
mov cx, di
MulTen: mul bx
loop MulTen
AddEach: add YearValue[0], ax
dec si
inc di
pop cx
loop DealEach
pop di
pop si
pop dx
pop cx
pop bx
pop ax
ret
JudgeLeap: ;; Judge whether is the leap year
;; 判断是否为闰年
push ax
push bx
push cx
push dx
push si
push di
mov ax, YearValue[0]
mov dx, 0
mov bx, 4
div bx
cmp dx, 0
jne NotLeap
mov ax, YearValue[0]
mov dx, 0
mov bx, 100
div bx
cmp dx, 0
je Div400
lea dx, IsLeapYear
mov ah, 09h
int 21h
jmp OK
Div400: mov ax, YearValue[0]
mov dx, 0
mov bx, 400
div bx
cmp dx, 0
jne NotLeap
lea dx, IsLeapYear
mov ah, 09h
int 21h
jmp OK
NotLeap: lea dx, NotLeapYear
mov ah, 09h
int 21h
OK: pop di
pop si
pop dx
pop cx
pop bx
pop ax
ret
code ends
end start
