Buscar

CS310-CH10 - Assembler Basics - Part1

Esta é uma pré-visualização de arquivo. Entre para ver o arquivo original

*
*
Assembler Basics
Western Illinois University
Department of Computer Science
CS310 – ST224
 Prof. Dr. Martins
Computer Organization 
& Architecture 
Created by Dr. Leff Modified by Dr. Martins
*
*
Assembler, Some basics
The Intel PC has several registers, each of which has a short name. A register is a collection of flip-flops. The computer's instructions will cause values to be latched into the register from RAM. 
Other instructions will cause values to go from the register to a memory location in RAM. Sometimes, arithmetic or logical operators will be performed between the values going to or from the RAM and the value already in the register. 
*
*
Assembler, some basics
The most important of these registers are AX, BX, CX, and DX. Each are sixteen bits. We can refer to the lower eights bits of each as AL, BL, CL and DL, respectively. We can refer to the upper eight bits of each as AH, BH, CH and DH. 
*
*
Assembler, some basics
We will also see that we can refer to memory locations or spots in the RAM by means of variable names.
*
*
The MOV instruction
The mov instruction takes several forms. 
mov register, memory copies data from memory to a register 
mov memory, register copies data from register to a memory location 
mov register1,register2 copies from register2 to register1 
*
*
Example
We will see these options are also available with other instructions including add and sub. 
Our first example also uses the inc instruction which increments (adds one) a register. 
The important lines are: 
mov ax,x 
inc ax 
mov x,ax 
Each of these instructions corresponds to bits in RAM, which are interpreted by the and gates and other digital logical inside the central processing unit. The assembler converts the lines into bits. 
*
*
Fields
Observe that each line consists of two to three fields:
label if present, this must begin on column one 
operator this must be at least one space from the label, if any. If there is no label on a line, then the operator must begin on or after column two. 
operands this must be one or more space from the operator. 
*
*
Remarks
A comment is introduced with a semicolon (;)--anything after the semicolon is ignored. 
Note, lining up one's operators and operands in a neat fashion is encouraged, especially by your boss should you do this in the real world. 
*
*
Remarks
However, other lines give information to the assembler to make this work. 
the form label dw value allocates two bytes in memory. It also converts value to binary and puts it in those values. 
The model tiny tells the computer that we are creating a .com file. This is the simplest case and only applies to those programs that are less than 64K for both our programs and data. Since I don't want to burden you with the concept of "segments," we will use the "tiny" model. 
*
*
Assembler directives
startupcode generates assembler instructions needed to start up a program under Windows or DOS. Until we get into segments, I won't discuss these. 
exitcode generates assembler instructions needed to return back to Windows. Treat it as a stop instruction
*
*
Assembler Directives
Assume it were missing; then after executing the move of the register ax back into memory location x, the computer would try and execute the number 1026 as if it were an instruction. I don't know what that opcode corresponds to, but it probably isn't useful or meaningful. 
Lastly, the end statement tells the assembler to quit processing lines into machine code. It is not a stop instruction. 
*
*
Another example
model tiny ; This program does c= a + b; 
.code 
startupcode 
 mov ax,a 
 add ax,b 
 mov c,ax 
 exitcode 
a dw 4 
b dw 5 
c dw 
 end ; The first line loads the 4 into the ax register for 
 ; processing. The second line adds the 5 to ax, giving 
 ; nine to ax. The third line moves ax back into c. 
*
*
Converting code with parentheses 
Sometimes, we want to do more elaborate operations represented by parentheses as illustrated by the example below: ; Q = C- (A+ B) 
 mov ax,a 
 add ax,b 
 mov bx,c 
 sub bx,ax 
 mov q,bx 
 exitcode 
a dw 4 
b dw 8 
c dw 64 
q dw 
*
*
Remarks
The program loads a+b into ax on the first two lines. Then, we move c into bx. We subtract (a+b) on the next line leaving c - (a+b) in BX we can then move the result to q, a memory location. 
Develop a systematic approach to converting Java containing multiple nested parentheses to assembler, work the parentheses from the inside out. Assign each parentheses to a different register (ax, bx, cx, dx). 
*
*
Remarks
You may need to use temporary memory locations if the parentheses are too numerous or too deep. 
You can use a sequence like:     mov cl,1     shl ax,cl to multiply the contents of ax register by two. The second instruction shifts ax one position to the left. This multiplies by two. Example, if we shift the binary number 101 one position to the left, we get 1010. These binary numbers represent five and ten, respectively. 
*
*
Example
Here is some code to multiply a by three: 
mov ax,a 
mov bx,a ; another copy of a 
mov cl,1
shl ax,cl ; ax now = a*2 
add bx,ax ; bx now = a*3 
mov b,bx ; store a*3 into the 
 ; variable b. 
*
*
Instructions to run the Turbo Assembler
Log in to your CS Server Account (or lab) in either Stipes 303 or Stipes 309. 
Start up the command prompt from the TASM icon CS CLASS on the desktop. 
Edit the file xxx.asm where xxx is any name you choose. You can use any editor as long as you save in ASCII. I use EDIT which saves in ascii by default. 
Type tasm xxx.asm 
Type tlink /t xxx.obj, xxx.com 
To test, type td xxxcom 
step through the program with the F8 key.

Teste o Premium para desbloquear

Aproveite todos os benefícios por 3 dias sem pagar! 😉
Já tem cadastro?

Outros materiais