汇编笔记example(0)

一个hello world程序汇编8086指令

name "hi-world";
;1;NAME 程序开始伪指令
;格式:NAME 模块名
;汇编语言将以模块名作为模块的名字

; this example prints out  "hello world!"
; by writing directly to video memory.
; in vga memory: first byte is ascii character, byte that follows is character attribute.
; if you change the second byte, you can change the color of
; the character even after it is printed.
; character attribute is 8 bit value,
; high 4 bits set background color and low 4 bits set foreground color.
; hex    bin        color
; 
; 0      0000      black
; 1      0001      blue
; 2      0010      green
; 3      0011      cyan
; 4      0100      red
; 5      0101      magenta
; 6      0110      brown
; 7      0111      light gray
; 8      1000      dark gray
; 9      1001      light blue
; a      1010      light green
; b      1011      light cyan
; c      1100      light red
; d      1101      light magenta
; e      1110      yellow
; f      1111      white
org 100h
; 设置显示器模式,入口参数为AH=00h,AL代表相应的显示器模式AL=03h代表80×25 16色 文本
mov ax, 3     ; text mode 80x25, 16 colors, 8 pages (ah=0, al=3)
int 10h       ; do it!

; cancel blinking and enable all 16 colors:取消闪烁
mov ax, 1003h
mov bx, 0
int 10h

; set segment register:设置段寄存器
mov     ax, 0b800h
mov     ds, ax

; print "hello world"
; first byte is ascii code, second byte is color code.
; 第一字节是字母,第二字节是字体颜色和背景颜色
mov [02h], 'H'
mov [04h], 'e'
mov [06h], 'l'
mov [08h], 'l'
mov [0ah], 'o'
mov [0ch], ','
mov [0eh], 'W'
mov [10h], 'o'
mov [12h], 'r'
mov [14h], 'l'
mov [16h], 'd'
mov [18h], '!'
; 对字体和背景的颜色进行循环赋值
; color all characters:
mov cx, 12  ; number of characters.
mov di, 03h ; start from byte after 'h'

c:  mov [di], 00001111b   ; light red(1100) on yellow(1110)
    add di, 2 ; skip over next ascii code in vga memory.
    loop c
    
; wait for any key press:
mov ah, 0
int 16h;输入输出中断,等待一个字符的输入

ret;返回控制权限

;关于输入输出中断int n
;其格式为首先修改AHAL的值,然后调用int 10h,完成一个修改,对屏幕
;显示或者其他方面进行设置

输出结果:
在这里插入图片描述

加减法并将结果转化为二进制直接输出
程序:

name "add-sub";命名
org 100h ;100h开始存储指令

mov al, 5       ; bin=00000101b ,al赋值为5
mov bl, 10      ; hex=0ah or bin=00001010b ,bl赋值为10

; 5 + 10 = 15 (decimal) or hex=0fh or bin=00001111b
add bl, al      ;bl=al+bl=15
                  
; 15 - 1 = 14 (decimal) or hex=0eh or bin=00001110b
sub bl, 1       ;bl=bl- 1=14

; print result in binary:用二进制打印运算结果
mov cx, 8               ;循环次数设置为8  
mov ah, 2   ; print function.打印函数
print: ;数字默认是十进制,在数字的尾部使用标识'b'或者'h'标识数字的进制情况
       mov dl, '0' ;把字符‘0’存储到dl中
       test bl, 10000000b  ; test first bit.看第一位是不是0
       jz zero             ;根据此时ALU中的数据ZF标志位(结果为0ZF=1否则ZF=0),为零跳转
       mov dl, '1'         ;否则赋值为1
zero:  int 21h             ;BIOS中断,此时AH数值为2,则
       shl bl, 1           ;INT 21h / AH=2 - write character to standard output.
                           ;entry: DL = character to write, after execution AL = DL.       
loop print

; print binary suffix:
mov dl, 10 ;换行之后跳到了下一行的相同位置,而不是从下一行的开始位置开始输出结果
int 21h
mov dl, 'b'
int 21h
; wait for any key press:
mov ah, 0   ;INT 16h / AH = 00h - get keystroke from keyboard (no echo).
int 16h     ;AH = BIOS scan code.   键盘的扫描码存储到AH;AL = ASCII character.  字符的ASCII码存储到AL;if a keystroke is present, it is removed from the keyboard 
            ;buffer.如果某一个键盘键被敲击了,那么它将会在键盘缓冲区中被删除
ret
;PS:"$"作用
;“$” 是汇编语言中的一个预定义符号,等价于当前正汇编到的段的当前偏移值。
;在汇编中是字符串结束的标志

输出结果:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值