【发布时间】:2013-12-26 14:39:40
【问题描述】:
所以,今天我尝试为我正在开发的操作系统创建一个库,它具有一个简单的功能:在屏幕上打印字符。要使用这个函数,我只需要将字符串地址压入堆栈并调用它(字符串必须以 0x00 字节结尾)。下面是函数的源代码:
__print: ;Print string that is terminated with a 0x00 to screen
__print_prepare:
pop si ;SI Register = String Address
mov ah, 0x0E ;0x0E Function = Print Character on screen
mov bx, 0x0F ;Background = Black, CharColor = White
xor cx, cx ;Counter = 0
__print_check:
push byte [cx + si] ;Push character to the stack
or byte [cx + si], byte [cx + si] ;Check if the byte equals 0x00
jz __print_exit ;If true then exit
jmp __print_main ;Else print the character
__print_main:
pop al ;Store the byte in the AL Register
int 0x10 ;Call interupt 0x10 = BIOS Interupt
jmp __print_next ;Continue to next character
__print_next:
inc cx ;Increment CX Register by one for next character = Counter
jmp __print_check ;Check authenticity of character
__print_exit:
ret
每当我尝试汇编源代码时,都会出现以下 nasm 错误:
def_os_lib.asm:10: 错误:操作码和操作数的组合无效
def_os_lib.asm:11: 错误:操作码和操作数的组合无效
def_os_lib.asm:16: 错误:操作码和操作数的组合无效
另外,在某些情况下,也就是我用ELF格式编译的时候,会提示这个错误:
def_os_lib.asm:10: 错误:地址大小的不可能组合
def_os_lib.asm:11: 错误:地址大小的不可能组合
我用于 nasm(bin) 的命令是:
nasm -f bin def_os_lib.asm
我用于 nasm(elf64) 的命令是:
nasm -f elf64 def_os_lib.asm
我刚刚开始组装,我知道我迈出了一大步。我只是想再深入一点。
感谢大家的帮助。通过纠正您的建议,我已经完成了源代码。这是新代码:
__print: ;Print string that is terminated with a 0x00 to screen
__print_prepare:
pop si ;SI Register = String Address
xor bx, bx ;Counter = 0
__print_check:
push bx ;Save BX
xor ax, ax ;AX = 0
add bx, si ;BX = Address of the character
mov al, byte [bx] ;AL = Character
pop bx ;Restore BX
push ax ;Save character
or ax, ax ;Check if the byte equals 0x00
jz __print_exit ;If true then exit
jmp __print_main ;Else print the character
__print_main:
pop ax ;Store the byte in the AL Register
push bx ;Save BX
mov bx, 0x0F ;Background = Black, CharColor = White
mov ah, 0x0E ;0x0E Function = Print Character on screen
int 0x10 ;Call interupt 0x10 = BIOS Interupt
jmp __print_next ;Continue to next character
__print_next:
pop bx ;Restore BX
inc bx ;Increment CX Register by one for next character = Counter
jmp __print_check ;Check authenticity of character
__print_exit:
ret
【问题讨论】:
标签: assembly operating-system nasm interrupt cpu-registers