一个存储器拥有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

汇编入门基础与helloworld的更多相关文章

  1. [.NET MVC4 入门系列01]Helloworld MVC 4 第一个MVC4程序

    [.NET MVC4 入门系列01]Helloworld MVC 4 第一个MVC4程序   一.练习项目: http://www.asp.net/mvc/tutorials/mvc-4/gettin ...

  2. Java入门基础(变量、操作符与表达式)

    Java入门基础 1. 第一个程序 2.变量(命名.运算.整数/小数/字符串.布尔类型) 3.操作符与表达式(算术/逻辑/关系/赋值/自增/类型转换操作符) HelloWorld! public cl ...

  3. mybatis入门基础(二)----原始dao的开发和mapper代理开发

    承接上一篇 mybatis入门基础(一) 看过上一篇的朋友,肯定可以看出,里面的MybatisService中存在大量的重复代码,看起来不是很清楚,但第一次那样写,是为了解mybatis的执行步骤,先 ...

  4. 01shell入门基础

    01shell入门基础 为什么学习和使用shell编程 shell是一种脚本语言,脚本语言是相对于编译语言而言的.脚本语言不需要编译,由解释器读取程序并且执行其中的语句,而编译语言需要编译成可执行代码 ...

  5. Markdown入门基础

    // Markdown入门基础 最近准备开始强迫自己写博文,以治疗严重的拖延症,再不治疗就“病入骨髓,司命之所属,无奈何”了啊.正所谓“工欲善其事,必先利其器”,于是乎在写博文前,博主特地研究了下博文 ...

  6. JavaScript入门基础

    JavaScript基本语法 1.运算符 运算符就是完成操作的一系列符号,它有七类: 赋值运算符(=,+=,-=,*=,/=,%=,<<=,>>=,|=,&=).算术运 ...

  7. C++ STL编程轻松入门基础

    C++ STL编程轻松入门基础 1 初识STL:解答一些疑问 1.1 一个最关心的问题:什么是STL 1.2 追根溯源:STL的历史 1.3 千丝万缕的联系 1.4 STL的不同实现版本 2 牛刀小试 ...

  8. HTML入门基础教程相关知识

    HTML入门基础教程 html是什么,什么是html通俗解答: html是hypertext markup language的缩写,即超文本标记语言.html是用于创建可从一个平台移植到另一平台的超文 ...

  9. GCC内联汇编入门

    原文为GCC-Inline-Assembly-HOWTO,在google上可以找到原文,欢迎指出翻译错误. 中文版说明 由于译者水平有限,故译文出错之处,还请见谅.C语言的关键字不译,一些单词或词组( ...

随机推荐

  1. 每天CSS学习之line-height

    line-height是CSS的一个属性,其作用是设置行高.其有以下几种值: 1.normal:自动设置合理的行间距.该值是默认值.如下示例: p{ line-height:normal; } 结果: ...

  2. 图的邻接矩阵存储实现,C++描述

    body, table{font-family: 微软雅黑; font-size: 13.5pt} table{border-collapse: collapse; border: solid gra ...

  3. :状态模式:GumballMachine

    #ifndef __STATE_H__ #define __STATE_H__ #include <iostream> #include<stdlib.h> using nam ...

  4. JavaScript+CSS+DIV实现表格变色示例

    <!DOCTYPE html> <html> <head> <title>colortable.html</title> <scrip ...

  5. Ubuntu16.04 python2.7升级python3.5

    正常情况下,你安装好ubuntu16.04版本之后,系统会自带 python2.7版本,如果需要下载新版本的python3.5,就需要进行更新.下面给出具体教程: 1.首先在ubuntu的终端tern ...

  6. 原生js封装tap

    // tap事件封装function tap(obj, callBack){ if(typeof obj != 'object') return; // 变量 var startTime = 0; / ...

  7. 2019-03-20-day015-序列化存储

    昨日回顾 序列化模块: json -- load dump dumps loads pickle -- load dump dumps loads shelve -- 文件 + 字典 f = shel ...

  8. Strassen algorithm(O(n^lg7))

    Let A, B be two square matrices over a ring R. We want to calculate the matrix product C as {\displa ...

  9. Python 默认值字典

    from collections import defaultdict # 默认值字典 dd = defaultdict(lambda: "胡辣汤") # callable 可调用 ...

  10. SpringBoot2静态资料访问

    在SpringBoot2内要继承WebMvcConfigurationSupport并重写addResourceHandlers方法才能访问到静态资料. @Configuration public c ...