汇编入门基础与helloworld

一个存储器拥有128个存储单元,可存储128个byte(字节),一个bite则又是由8个二进制位即bit(比特)组成,bit是计算机的最小信息单位。

总线分为地址总线,控制总线,数据总线

一个cpu有n根地址线,则说这个cpu的地址总线宽度为n,该cpu最多能寻找2的n次方个内存单元

8根数据总线可传达1个8位二进制数据,即一个字节

通用寄存器:ax,bx,cx,dx,(不区分大小写)其中每个寄存器都可以分为两个可独立使用的8位寄存器,如ax可分为ah和al,ah放高位字节,al放低位字节

十六进制数在后面加H,二进制加B

mov ax,18 把18放到ax中
mov ah,18把18放到ah中
mov ax,bx把bx的数据放到ax中
add ax,bx把ax+bx的结果放入ax中
下面这种情况需要注意
如果mov ah,99
add ah,90因为ah是8位寄存器,明显9+9已经大于15了没有位置可以放了,所以多余的1不会被保存(但cpu也不会丢弃这个数据)

物理地址的计算方法:段地址×16+偏移地址

段寄存器:cs,ds,ss,es,其中cs是代码段寄存器

ip为指令指针寄存器,cpu将cs:ip指向的内容当作指令执行,只需要吧cs当作段地址,ip当作偏移地址即可算出相应的命令物理地址,注意ip在执行玩一条语句后会自动增加刚刚执行完的那条指令内容的长度

直接修改cs和ip: jmp 段地址:偏移地址
例如 jmp 2AE3:3 直接从2AE33处读取指令
如果仅想修改ip jmp 寄存器
例如 jmp ax 会直接把原ip修改为ax的值

废话少说,直接上干货,没有helloworld的入门教程都是耍流氓

// hello.asm

SECTION .data                    ; Section containing initialised data 定义段
         HelloMsg:db "Hello World!",10 ;定义一个字符串
         HelloLen:equ $-HelloMsg   ;求其长度 
SECTION .text                   ; Section containing code定义代码段
global     _start                 ; Linker needs this tofind the entry point! 定义一个程序入口,类似c语言的main函数
_start: ;接下来就是具体函数内容了
         mov eax,4                  ; Specify sys_writecall
         mov ebx,1                  ; Specify FileDescriptor 1: Standard Output
         mov ecx,HelloMsg    ; Pass offset ofthe message
         mov edx,HelloLen     ; Pass the lengthof the message
         int 80H                        ; Make kernelcall,软中断,陷入内核态
         mov eax,1                  ; Code for ExitSyscall
         mov ebx,0                  ; Return a code ofzero   
         int 80H                        ; Make kernelcall

再给个别人的教程吧

; Hello World Program - asmtutor.com
; Compile with: nasm -f elf helloworld.asm
; Link with (64 bit systems require elf_i386 option): ld -m elf_i386 helloworld.o -o helloworld
; Run with: ./helloworld

SECTION .data
msg     db      'Hello World!', 0Ah

SECTION .text
global  _start

_start:

   mov     edx, 13     ; number of bytes to write - one for each letter plus 0Ah (line feed character)
    mov     ecx, msg    ; move the memory address of our message string into ecx
    mov     ebx, 1      ; write to the STDOUT file
    mov     eax, 4      ; invoke SYS_WRITE (kernel opcode 4)
    int     80h

    mov     ebx, 0      ; return 0 status on exit - 'No Errors'
    mov     eax, 1      ; invoke SYS_EXIT (kernel opcode 1)
    int     80h

那么程序写好了该怎么运行呢?
首先nasm -g -f elf64 hello.asm (-g 是加入调试信息,否则无法使用gdb等调试工具调试,-f 用来说明格式的,elf64是因为我的系统是64位的,如果你的是32位,需要改为elf32,默认会生成hello.o)
然后ld -e _start -o hello hello.o (注意-e _start 选项,提示无法找到入口需要加入这个选项,如果没有提示不加也可以)
下面是我的操作过程

root@BP:~# nasm -g -f elf64 hello.asm
root@BP:~# ld -o hello hello.o
root@BP:~# ./hello
Hello World!

root@BP:~# gdb hello
GNU gdb (Debian 7.12-6) 7.12.0.20161007-git
Copyright (C) 2016 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from hello...done.
(gdb) l
1   SECTION .data                    ; Section containing initialised data
2            HelloMsg:db "Hello World!",10
3            HelloLen:equ $-HelloMsg       
4                       ; Section containing uninitialized data    
5   SECTION .text                   ; Section containing code
6   global     _start                 ; Linker needs this tofind the entry point!
7   _start:
8            mov eax,4                  ; Specify sys_writecall
9            mov ebx,1                  ; Specify FileDescriptor 1: Standard Output
10           mov ecx,HelloMsg    ; Pass offset ofthe message
(gdb) 

剩下来的就是使用gdb进行各种操作了,请查看我的另一篇文章gdb入门
http://blog.csdn.net/qq_34829953/article/details/71385472

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值