start your world here

本文介绍了一种在Linux环境下使用GCC进行裸机编译的方法,并提供了一个具体的编译命令示例。该命令用于编译一个名为emy_mainpure_code.c的C文件,此文件包含一个使用汇编指令实现的my_main函数,类似于exit(0)的功能。
平台
linux

编译命令
 gcc -Wall -g -fno-builtin -ffreestanding -nostdlib -nostartfiles -e my_main pure_code.c


 
代码
void my_main(void)
{
    asm volatile ("mov $0, %ebx\n\t"
                  "mov $1, %eax\n\t"
                  "int $0x80\n\t"); // like exit(0)
}


请分析以下MIPS代码:#include <asm/asm.h> .data str: .asciiz "Hello World\n" # Null-terminated string "Hello World" stored at label 'str' .align 2 # align to 4-byte boundary (2^2) var: .byte 3 # correctly aligned byte: 3 /* '<x>' in the comments is the part to be replaced. */ /* use '.align <x>' to align the following words to 1-byte boundary (disabling word-alignment) */ /* so that the byte 3 and word 7 is "connected" */ /* Your code here. (1/6) */ .align 1 .word 7, 8, 9 .text /* We define '_start_mips' here as the entry of our program. */ EXPORT(_start_mips) .set at .set reorder mtc0 zero, CP0_STATUS li sp, 0x84000000 /* Load the address of the string 'str' into the first parameter register. */ la a0, str /* use 'addiu sp, sp, <x>' to push a proper-sized frame onto the stack for Nonleaf function 'print_str'. */ /* Your code here. (2/6) */ addiu sp, sp, -4 jal print_str /* use 'addiu sp, sp, <x>' to restore stack pointer. */ /* Your code here. (3/6) */ addiu sp, sp, 4 /* Set the first four parameters. */ li a0, 0 li a1, 1 li a2, 2 li a3, 3 /* use 'addiu sp, sp, <x>' to push a proper-sized frame onto the stack for Nonleaf function 'hello'. */ /* Your code here. (4/6) */ addiu sp, sp, -20 lw t1, var li t2, 5 /* use 'sw t1, <x>(sp)' to store t1 at the proper place of the stack */ /* so that t1 is 5th argument of function hello. */ /* Your code here. (5/6) */ sw t1, 16(sp) /* use 'sw t2, <x>(sp)' to store t2 at the proper place of the stack */ /* so that t2 is 6th argument of function hello. */ /* Your code here. (6/6) */ sw t2, 20(sp) /* use 'j' to call the function 'hello', we use 'j' instead of 'jal' because 'hello' is 'noreturn' */ j hello
03-12
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值