⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 anasm.asm

📁 这是汇编器的源代码
💻 ASM
📖 第 1 页 / 共 5 页
字号:
section code

section data

section stack stack
resb 512
top_of_stack

section end_progg

global msg_info,failure,mem_offset,mem_label,mem_fileout,line,act_org,print_error,msg_short_jmp_range,pointer_write

%include "extern.inc"
%include "version.inc"

section code
..start
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
start:
	mov ax,data
	mov es,ax

	call init_memory
	cmp ax,0xffff
	je near .end

	mov si,msg_startup
	call print

	mov di,buffer_cmdline
	call get_cmdline

	cmp ax,0xffff
	je near .end

	mov si,buffer_cmdline
	call parse_cmdline

	cmp ax,0xffff
	je near .end

	xor al,al
	mov dx,word[file_in]
	call open_file
	cmp ax,0xffff
	je near .end
	mov word[file_in_handle],ax

	mov bx,0x1000
	call alloc_memory
	cmp ax,0xffff
	je near .end
	mov word[mem_filein],ax

	call alloc_memory
	cmp ax,0xffff
	je near .end
	mov word[mem_offset],ax

	call alloc_memory
	cmp ax,0xffff
	je near .end
	mov word[mem_label],ax

	call alloc_memory
	cmp ax,0xffff
	je near .end
	mov word[mem_fileout],ax

	mov bx,word[file_in_handle]
	mov cx,0xfffe
	xor dx,dx
	push ds
	mov ds,word[mem_filein]
	call read_file
	pop ds
	cmp ax,0xffff
	je near .end

	push es
	mov es,[mem_filein]
	mov si,ax
	mov byte[es:si],0
	pop es

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
.loop_pass1
	mov byte[failure],0

	call create_label_table

	cmp byte[failure],0x0d
	je short .end_pass1

.loop_pass1@nextline
	inc word[lines]
	call next_line

	jmp short .loop_pass1
.end_pass1
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
	cmp byte[failure],0xff
	je near .end

	mov word[pointer_read],0
	mov word[act_label],-1
	dec word[lines]
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
.loop_pass2
	mov byte[failure],0

	call asm_line

	cmp byte[failure],0x0d
	je short .end_pass2

.loop_pass2@nextline
	call next_line
	inc word[line]

	jmp short .loop_pass2
.end_pass2
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
	cmp byte[failure],0xff
	je near .end

	test byte[options],10000000b
	je .loop_pass3

	mov si,msg_label_table
	call print
	call print_label_table
	mov si,msg_newline
	call print
	mov si,msg_offset_table
	call print
	call print_offset_table
	mov si,msg_newline
	call print

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
.loop_pass3
	call create_fileout
.end_pass3
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
	cmp al,0xff
	je .end

	mov si,[file_out]
	cmp si,0
	jne .file_out_ok

	mov si,[file_in]

.loop_file_out
	lodsb

	cmp al,'.'
	jne .loop_file_out

	dec si
	mov byte[ds:si],0

	mov ax,word[file_in]
	mov word[file_out],ax

.file_out_ok

	mov dx,[file_out]
	xor cx,cx
	call create_file
	cmp ax,0xffff
	je .end

	mov [file_out_handle],ax

	mov bx,ax
	mov cx,word[pointer_write]
	push ds
	mov ds,[mem_fileout]
	xor dx,dx
	call write_file
	pop ds
	cmp ax,0xffff
	je .end

	mov bx,[file_out_handle]
	call close_file

	mov si,msg_newline
	call print

	mov ax,word[lines]
	mov si,lines_str
	call bin2decascii
	call print

	mov si,msg_lines_asm
	call print

.end
	mov bx,word[file_in_handle]
	call close_file

	mov es,[mem_label]
	call dealloc_memory

	mov es,[mem_filein]
	call dealloc_memory

	mov es,[mem_offset]
	call dealloc_memory

	mov es,[mem_fileout]
	call dealloc_memory

	mov ax,4c00h
	int 21h
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
init_memory:
	pusha
	push es

	mov ax,ds
	push es
	pop ds
	mov es,ax

	mov bx,end_progg
	sub bx,ax

	call resize_memory

	cmp ax,0xffff
	je .end_failure

.end
	pop es
	popa
	ret
.end_failure
	pop es
	popa
	mov ax,0xffff
	ret
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;	Input:
;;	SI = pointer to buffer of the cmdline (first word have to be the number of arguments)
parse_cmdline:
	pusha

	lodsw
	mov cx,ax
.loop_parse
	lodsb

	cmp al,'-'
	je .cmd


	dec si
	and al,al
	jz .end_loop_parse

	mov word[file_in],si
.loop_filein
	lodsb

	and al,al
	jnz .loop_filein

	jmp short .end_loop_parse
.cmd
	lodsb

	cmp al,'o'
	je .cmd_out

	cmp al,'h'
	je .cmd_help

	cmp al,'d'
	je .cmd_debug

.cmd_unkwn
	mov si,msg_unknown_para
	call print

	jmp short .end_failure
.cmd_out
	lodsb

	and al,al
	jz .cmd_out1

	dec si
	mov word[file_out],si

	jmp .cmd_out_loop
.cmd_out1
	lodsb

	and al,al
	jz .cmd_out_error

	dec si
	mov word[file_out],si

.cmd_out_loop
	lodsb

	and al,al
	jnz .cmd_out_loop

	jmp .end_loop_parse
.cmd_out_error
	mov si,msg_incomplete_para
	call print
	jmp short .end_failure
.cmd_help
	mov si,msg_info
	call print
	jmp .end_failure
.cmd_debug
	or byte[options],10000000b
	inc si

	jmp short .end_loop_parse

.end_loop_parse
	dec cx
	and cx,cx
	jnz near .loop_parse

	mov si,[file_in]
	cmp si,0
	je .no_in

.end
	popa
	ret
.no_in
	mov si,msg_no_in
	call print
.end_failure
	popa
	mov ax,0xffff
	ret
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; 	IN :
;;	SI=pointer to error message
;;	AX=line where error is
print_error:
	pushad

	push si
	mov si,word[file_in]
	call print

	mov dl,':'
	call printc

	mov si,lines_str
	call bin2decascii
	call print

	mov dl,':'
	call printc

	mov si,msg_error
	call print

	pop si
	call print
.end
	popad
	ret
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
next_line:
	pusha

	mov fs,word[mem_filein]
	mov si,word[pointer_read]

	jmp .loop_first
.loop
	inc word[pointer_read]
.loop_first
	mov al,byte[fs:si]
	inc si

	and al,al
	jz short .end_file
	cmp al,13
	je .test_nextline

	jmp short .loop
.test_nextline
	mov al,byte[fs:si]

	cmp al,10
	jne .end

	inc word[pointer_read]

.end
	inc word[pointer_read]
	popa
	ret
.end_file
	mov byte[failure],0x0d
	popa
	ret
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;	Input :
;;	SI = pointer to label string (0-terminated)
add_label2table:
	pusha
	push es

	mov es,[mem_label]
	mov di,word[label_free]

	add di,2
	mov ax,-1
	stosw

.loop
	lodsb

	stosb
	and al,al
	jnz short .loop

	mov ax,word[label_free]
	mov [label_free],word di

	push di
	mov di,ax
	pop ax
	stosw

.end
	pop es
	popa
	ret
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;	Input :
;;	CX = offset (where label is)
;;	AX = offset (of the label)
add_offset2label:
	pusha
	push es

	mov es,[mem_label]
	mov di,cx

	add di,2
	stosw

.end
	pop es
	popa
	ret
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;	Input :
;;	SI = pointer to label string (0-terminated)
;;	Output :
;;	AL = 0 / BX = label offset / CX = pointer to label
;;	AL = 1 (no label found)
look4label:
	push di
	push si
	push es

	mov es,[mem_label]
	xor di,di
	xor bx,bx

	lodsb
	dec si

	cmp al,'.'
	jnz .loop_local

	mov di,word[act_label]
	mov bx,word[es:di]
.loop_no_local
	mov di,bx

	mov bx,word[es:di]

	cmp di,word[label_free]
	je .end_not

	add di,4

	mov al,byte[es:di]
	cmp al,'.'
	jne .end_not

	call string_cmp

	and ax,ax
	jnz .loop_no_local
	jz .end

.loop_local
	mov di,bx

	mov bx,word[es:di]

	cmp di,word[label_free]
	je .end_not

	add di,4

	call string_cmp

	and ax,ax
	jnz .loop_local

.end
	sub di,4
	mov cx,di
	add di,2
	mov bx,word[es:di]
	xor al,al

	pop es
	pop si
	pop di
	ret
.end_not
	mov al,1

	pop es
	pop si
	pop di
	ret
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
create_label_table:
	pusha

	mov fs,[mem_filein]
	mov si,word[pointer_read]
	mov di,buffer_opcode
	call parse_line

	add word[pointer_read],cx

	cmp byte[failure],0
	jne near .end

	mov si,buffer_opcode
	call string_len

	add si,cx
	dec si
	lodsb
	cmp al,':'
	je .label_dpoint

	sub si,cx
	lodsb
	dec si
	cmp al,'.'
	je .label_point

	mov si,word[pointer_read]
	mov di,buffer_bef1
	call parse_line

	cmp byte[failure],0
	jne .end

	mov si,op_db
	call string_cmp
	and ax,ax
	jz .label_var

	mov si,op_dw
	call string_cmp
	and ax,ax
	jz .label_var

	mov si,op_dd
	call string_cmp
	and ax,ax
	jnz .end

.label_var
	mov si,buffer_opcode

	call look4label
	and al,al
	jz .error_label_is_there

	call add_label2table

	jmp short .end
.label_point
	cmp word[act_label],-1
	je .error_no_local_label

	call look4label
	and al,al
	jz .error_label_is_there

	call add_label2table

	jmp short .end
.label_dpoint
	dec si
	mov byte[si],0
	inc si
	sub si,cx

	call look4label
	and al,al
	jz .error_label_is_there

	call add_label2table

	call look4label

	mov word[act_label],cx

.end
	popa
	ret
.error_no_local_label
	mov ax,word[lines]
	mov si,msg_no_local_label
	call print_error
	mov byte[failure],0xff
	popa
	ret
.error_label_is_there
	mov ax,word[lines]
	mov si,msg_label_is_there
	call print_error
	mov byte[failure],0xff
	popa
	ret
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
print_label_table:
	pusha

	mov fs,[mem_label]
	xor si,si

.loop
	cmp si,word[label_free]
	je near .end

	add si,2
	mov bx,word[fs:si]
	add si,2
	mov di,buffer_opcode
.loop_label
	mov al,byte[fs:si]
	inc si

	stosb

	and al,al
	jz .loop_end

	jmp .loop_label
.loop_end
	push si
	mov si,buffer_opcode
	call print
	mov dl,9
	call printc
	mov ax,bx
	call hex2ascii16bit_str
	mov si,msg_newline
	call print
	pop si
	jmp .loop

.end
	popa
	ret
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
print_offset_table:
	pusha
	push es

	mov es,[mem_offset]
	xor di,di

.loop
	cmp di,word[offset_free]
	je near .end
	mov al,byte[es:di]
	inc di

	mov si,msg_newline
	call print

.loop_type1
	cmp al,1
	jne .loop_type2

	mov ax,word[es:di]
	add di,2

	call hex2ascii16bit_str
	mov dl,' '
	call printc

	mov ax,word[es:di]
	add di,2

	call hex2ascii16bit_str

	jmp .loop
.loop_type2
	cmp al,2
	jne .loop_type3

	mov ax,word[es:di]
	add di,2

	call hex2ascii16bit_str
	mov dl,' '
	call printc

	mov ax,word[es:di]
	add di,2

	call hex2ascii16bit_str
	mov dl,' '
	call printc

	mov ax,word[es:di]
	add di,2

	call hex2ascii16bit_str

	jmp .loop
.loop_type3
	mov ax,word[es:di]
	add di,2

	call hex2ascii16bit_str
	mov dl,' '
	call printc

	mov ax,word[es:di]
	add di,2

	call hex2ascii16bit_str
	mov dl,' '
	call printc

	mov ax,word[es:di]
	add di,2

	call hex2ascii16bit_str
	mov dl,' '
	call printc

	mov ax,word[es:di]
	add di,2

	call hex2ascii16bit_str

	jmp .loop

.end
	pop es
	popa
	ret
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
asm_line:
	pushad

	mov gs,[mem_fileout]
	mov fs,[mem_filein]
	mov si,word[pointer_read]
	mov di,buffer_opcode
	call parse_line
	add word[pointer_read],cx

	cmp byte[failure],0
	jne near .end

	mov si,buffer_opcode
	lodsb
	dec si

	cmp al,'.'
	je .label_point

	call string_len
	add si,cx
	dec si
	lodsb

	cmp al,':'
	je .label_dpoint

	mov si,word[pointer_read]
	mov di,buffer_bef1
	call parse_line

	cmp byte[failure],0
	jne .end_label_test

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -