俺啥也不懂-不知道什么时候才会编写操作系统-一个hello world写一年-汇编如何引用其它文件

反正就是hello world,先看代码

hello.asm

%include 'tools.asm'
section .data
msg db 'hello world',0Ah
section .text
global _start
_start:
	mov eax,msg	;将msg放入eax寄存器,用来计算长度
	call print	;调用print方法
	call quit

tools.asm

strlen:
	push ebx
	mov  ebx,eax	;将msg存入ebx,开始通过eax自增计算msg的长度

nextchar:
	cmp  byte[eax],0	;这个以前的文章讲过,gdb调试那一章
	jz   finished
	inc  eax		
	jmp  nextchar

finished:
	sub  eax,ebx	;计算长度后,将长度存在eax中
	pop  ebx
	ret

print:
	push edx	;push指令将edx压入栈中,需要用到edx原始值得时候,使用pop指令出栈即可,下面相同
	push ecx
	push ebx
	push eax
	call strlen
	
	mov edx,eax	;
	pop eax		;重新使eax内存地址指向的值为msg
	
	mov ecx,eax	;固定将msg放入ecx
	mov ebx,1
	mov eax,4
	int 80h
	
	pop ebx
	pop ecx
	pop edx
	ret

quit:
	mov ebx,0
	mov eax,1
	int 80h
	ret
	

诸位,可以对比一下最初的hello world和这一章的区别,除了固定输出和固定退出,以及计算字符长度,没增加什么东西。

nasm -f elf -g hello.asm -o hello.o
ld -m elf_i386 hello.o -o main

写到此处,俺决定沉寂一阵子,好好学习一下

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值