📄 asm_tutorial_02.html
字号:
<!doctype HTML public "-//W3O//DTD W3 HTML 3.0//EN"><HTML><HEAD><TITLE>8086 Assembler Tutorial for Beginners (Part 2)</TITLE><META name="description" content="Memory Access using 8086 Assembler"><META name="keywords" content="memory access, 8086, tutorial, programming, assembler tutorial, tutorial for begginers"><META name="robots" content="nofollow"></HEAD><BODY bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink="#007099" alink="#FF0000"><TABLE WIDTH=80%><TR><TD><FONT FACE="Verdana" SIZE=3><FONT SIZE=+1><B>8086 Assembler Tutorial for Beginners (Part 2)</B></FONT><BR><BR><FONT SIZE=+2><B>Memory Access</B></FONT><BR><BR>To access memory we can use these four registers: <B>BX, SI, DI, BP</B>.<BR>Combining these registers inside <B>[ ]</B> symbols, we can get different memory locations.These combinations are supported (addressing modes):<BR><BR><FONT FACE="Fixedsys"><TABLE BORDER=1 COLS=3 CELLPADDING=10><TR><TD>[BX + SI]<BR>[BX + DI]<BR>[BP + SI]<BR>[BP + DI]<BR></TD><TD>[SI]<BR>[DI]<BR>d16 (variable offset only)<BR>[BX]<BR></TD><TD>[BX + SI] + d8<BR>[BX + DI] + d8<BR>[BP + SI] + d8<BR>[BP + DI] + d8<BR></TD></TR><TR><TD>[SI] + d8<BR>[DI] + d8<BR>[BP] + d8<BR>[BX] + d8<BR></TD><TD>[BX + SI] + d16<BR>[BX + DI] + d16<BR>[BP + SI] + d16<BR>[BP + DI] + d16<BR></TD><TD>[SI] + d16<BR>[DI] + d16<BR>[BP] + d16<BR>[BX] + d16<BR></TD></TR></TABLE></FONT><BR><B>d8</B> - stays for 8 bit displacement.<BR><BR><B>d16</B> - stays for 16 bit displacement.<BR><BR>Displacement can be a immediate value or offset of a variable, or even both.It's up to compiler to calculate a single immediate value.<BR><BR>Displacement can be inside or outside of <B>[ ]</B> symbols, compilergenerates the same machine code for both ways.<BR><BR>Displacement is a <B>signed</B> value, so it can be both positive or negative.<BR><BR>Generally the compiler takes care about differencebetween <B>d8</B> and <B>d16</B>, and generates the required machine code.<BR><BR><BR>For example, let's assume that <B>DS = 100</B>, <B>BX = 30</B>, <B>SI = 70</B>.<BR>The following addressing mode:<B>[BX + SI] + 25</B><BR>is calculated by processor to this physical address: <NOBR><B>100 * 16 + 30 + 70 + 25 = 1725</B>.</NOBR><BR><BR>By default <B>DS</B> segment register is used for all modesexcept those with <B>BP</B> register, for these <B>SS</B> segmentregister is used.<BR><BR>There is an easy way to remember all those possible combinations using this chart:<BR><BR><IMG SRC="addressing_mode.gif"><BR>You can form all valid combinations by taking only one item from each column or skippingthe column by not taking anything from it. As you see <B>BX</B> and <B>BP</B> never gotogether. <B>SI</B> and <B>DI</B> also don't go together. Here is an example of a validaddressing mode: <B>[BX+5]</B>.<BR><BR><BR><HR><BR><BR>The value in segment register (CS, DS, SS, ES) is called a "<B>segment</B>",<BR>and the value in purpose register (BX, SI, DI, BP) is called an "<B>offset</B>".<BR>When DS contains value <B>1234h</B> and SI contains the value <B>7890h</B> it can be also recordedas <B>1234:7890</B>. The physical address will be 1234h * 10h + 7890h = 19BD0h.<BR><BR><BR><HR><BR><BR>In order to say the compiler about data type,<BR>these prefixes should be used:<BR><BR><B>BYTE PTR</B> - for byte.<BR><B>WORD PTR</B> - for word (two bytes).<BR><BR>For example:<BR><PRE><FONT FACE="Fixedsys">BYTE PTR [BX] ; byte access. orWORD PTR [BX] ; word access.</FONT></PRE><I>Emu8086</I> supports shorter prefixes as well:<BR><BR><B>b.</B> - for <B>BYTE PTR</B><BR><B>w.</B> - for <B>WORD PTR</B><BR><BR>sometimes compiler can calculate the data type automatically,but you may not and should not rely on that when one of theoperands is an immediate value.<BR><BR><BR><HR><BR><FONT SIZE=+2><B><U>MOV</U> instruction</B></FONT><BR><BR><UL> <LI> Copies the <B>second operand</B> (source) to the <B>first operand</B> (destination).<BR><BR> </LI> <LI> The source operand can be an immediate value, general-purpose register or memory location.<BR><BR> </LI> <LI> The destination register can be a general-purpose register, or memory location.<BR><BR> </LI> <LI> Both operands must be the same size, which can be a byte or a word. </LI></UL><TABLE BORDER=1 CELLPADDING=10 WIDTH=100%><TR><TD>These types of operands are supported:<BR><BR><BLOCKQUOTE><FONT FACE="Fixedsys"> MOV REG, memory<BR> MOV memory, REG<BR> MOV REG, REG<BR> MOV memory, immediate<BR> MOV REG, immediate</FONT></BLOCKQUOTE><B>REG</B>:<FONT SIZE=-1> AX, BX, CX, DX, AH, AL, BL, BH, CH, CL, DH, DL, DI, SI, BP, SP.</FONT><BR><BR><B>memory</B>: [BX], [BX+SI+7], variable, etc...<BR><BR><B>immediate</B>: 5, -24, 3Fh, 10001101b, etc...<BR><BR></TD></TR></TABLE><BR><TABLE BORDER=1 CELLPADDING=10 WIDTH=100%><TR><TD>For segment registers only these types of <B>MOV</B> are supported:<BR><BR><BLOCKQUOTE><FONT FACE="Fixedsys"> MOV SREG, memory<BR> MOV memory, SREG<BR> MOV REG, SREG<BR> MOV SREG, REG<BR></FONT></BLOCKQUOTE><B>SREG</B>: DS, ES, SS, and only as second operand: CS.<BR><BR><B>REG</B>:<FONT SIZE=-1> AX, BX, CX, DX, AH, AL, BL, BH, CH, CL, DH, DL, DI, SI, BP, SP.</FONT><BR><BR><B>memory</B>: [BX], [BX+SI+7], variable, etc...<BR><BR></TD></TR></TABLE><BR>The <B>MOV</B> instruction <U>cannot</U> be used to set the value ofthe <B>CS</B> and <B>IP</B> registers.<BR><BR><TABLE BORDER=1 CELLPADDING=10 WIDTH=100%><TR><TD>Here is a short program that demonstrates the use of <B>MOV</B> instruction:<BR><BR><PRE><FONT FACE="Fixedsys">#MAKE_COM# ; instruct compiler to make COM file.ORG 100h ; directive required for a COM program.MOV AX, 0B800h ; set AX to hexadecimal value of B800h.MOV DS, AX ; copy value of AX to DS.MOV CL, 'A' ; set CL to ASCII code of 'A', it is 41h.MOV CH, 01011111b ; set CH to binary value.MOV BX, 15Eh ; set BX to 15Eh.MOV [BX], CX ; copy contents of CX to memory at B800:015ERET ; returns to operating system.</FONT></PRE></TD></TR></TABLE><BR><BR>You can <B>copy & paste</B> the above program to <I>Emu8086</I> code editor,and press [<B>Compile and Emulate</B>] button (or press <B>F5</B> key on your keyboard).<BR><BR>The Emulator window should open with this program loaded, click<NOBR>[<B>Single Step</B>]</NOBR>button and watch the register values.<BR><BR><BR>How to do <B>copy & paste</B>: <BR><OL><LI>Select the above text using mouse, click before the textand drag it down until everything is selected.<BR><BR></LI><LI>Press <B>Ctrl + C</B> combination to copy.<BR><BR></LI><LI>Go to <I>Emu8086</I> source editor and press <B>Ctrl + V</B>combination to paste.</LI></OL><BR><BR>As you may guess, "<B>;</B>" is used for comments, anything after "<B>;</B>" symbolis ignored by compiler.<BR><BR><BR>You should see something like that when program finishes:<BR><BR><IMG SRC="screen01.gif" WIDTH=426 HEIGHT=174><BR><BR>Actually the above program writes directly to video memory, so you may see that <B>MOV</B>is a very powerful instruction.<BR><BR><BR><HR><CENTER><A HREF="asm_tutorial_01.html"><B> <<< Previous Part <<< </B></A> <A HREF="asm_tutorial_03.html"><B> >>> Next Part >>> </B></A></CENTER><HR><BR></FONT></TD></TR></TABLE></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -