Aim:
1、ASM language 第三章 Fundamentals
2、構建UHF Demo 的架構
Learn:
1、ASM Fundamentals
.data: 指示爲變量區
.code: 指示爲代碼區
name PROC: 指示一個進程的開始
Instruction(指引)爲一段描述,在程序加載到內存並啓動後由處理器執行.包含:
Label(optional)
Instruction mnemonic(需要)
Operand(s)(常常需要)
Comment(optional)
1.1 Code Labels必須以:爲結束符,如:
;顯示Code label的使用方式
target:
mov ax, bx
...
jmp target
1.2 Data Labels不能以:爲結束符,如:
first BYTE 10
2 Insruction Mnemonic:主要有mov, add, sub, mul, jmp 和call(調用進程)
3 Operands: 主要有0和三種操作(有可能爲寄存器):內存操作,常量表達式和I/O端口
stc ;set Carry flag
inc ax ;add 1 to ax
4 Comments:
單行的評論以;爲開頭
塊評論,以COMMENT 和一個用戶定義的符號 爲開始,直到 用戶定義的符號出現爲止.如:
COMMENT !
This line is a comment
This line is also a comment
!
或者
COMMENT &
This line is a comment
This line is also a comment
&
Assemble-Link-Execute週期
1、編碼編寫一個ASCII源文件
2、assembler讀取源文件,機器語言翻譯此程序,生產一個object文件。Optionally,它產生一個鏈表文件。若生產錯誤則返回1.
3、Linker讀取object文件並查看它是否需要調用link庫中的程序並拷貝所有需要的程序,同object文件一起生產一個執行文件。自動生成一個map文件。
4、操作系統加載執行文件到內存,將CPU執行程序起始地址並執行。
在Protected模式下執行會編譯和鏈接,使用DOS命令:make32 progname(make32.bat必須防止在ASM文件同一路徑或者系統路徑下)
如果要在Real-address編譯,使用make16編譯和鏈接
Listing File包含源碼的拷貝,適用於打印行數,偏移地址,翻譯成機器語言和符號表。
定義:
value1 BYTE 'A'
value2 BYTE 233
list BYTE 10,20,30,40 ;鏈表:10在offset 0處,20在offset 1處,。。。
String以null byte爲結尾符,如:
greeting1 BYTE "Good night", 0
myMessage BYTE "Welcom to the demo program "
BYTE "created by his", 0dh, 0ah ;0dh,0ah表示換行
BYTE "send me a copy", 0dh, 0ah, 0
myMessage = "Welcom to the demo program created by his/nsend me a copy/n"
BYTE 20 DUP(0) ;20bytes,全爲0
BYTE 20 DUP(?) ;20bytes,全未初始化
BYTE 4 DUP("STACK") ;20bytes,"STACKSTACKSTACKSTACK"
var DWORD 20 DUP(?) ;unsigned array,且未初始化
12345678h
Litter Endian Order:0000, 0001, 0002, 0003分別爲78 56 34 12
Big Endian Order:0000, 0001, 0002, 0003分別爲12 34 56 78
編碼時使用字符替代數據,便於編譯和替代
$ - 表示數據的大小,如:
list BYTE 10,20,30,40
ListSize = ($ - list)
EQU:宏定義
name EQU expression
name EQU symbol
name EQU <test>
如:PI EQU <3.1415926>
matirx1 EQU 10*10
M1 WORD matrix1 ;WORD M1 = matrix1
Questions:
1、如何查看Listing File的內容
2、CRC爲何做異或時不需要X16上的數據,初始值的設置有什麼意義
AddSub.asm
TITLE Add and Subtract (AddSub.asm)
COMMENT !
Description:
This program add and subtract 32-bit integers
Author:
dl
Cretion Date:
2010-7-27 10:00:21
Revisions:
date:
Modified by:
!
;INCLUDE Irvine32.inc
;Use the next words to replace the above line
.386 ;identify the minimum CPU required for this program(Intel386)
.MODEL flat,stdcall ;instruct the assembler to generate code for a protected mode program
;stdcall enables the calling of MS-Windows functions
.STACK 4095
ExitProcess PROTO, dwEixtCode:DWORD ;ExitProcess is an MS-Windows function,used to halt the process
DumpRegs PROTO
.code
main PROC
call DumpRegs
mov eax, 10000h
add eax, 40000h
sub eax, 20000h
call DumpRegs ;Display Registers, is a procedure from Irvine32 link library
;exit
INVOKE ExitProcess, 0 ;return zero
main ENDP
END main