computing machinery intel x 86 - assembly language

47
Computing Machinery Intel x86 - Assembly Language

Upload: nathan-french

Post on 17-Dec-2015

232 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Computing Machinery Intel x 86 - Assembly Language

Computing MachineryIntel x86 - Assembly Language

Page 2: Computing Machinery Intel x 86 - Assembly Language

x86 Intel Series Microprocessor Registers

Page 3: Computing Machinery Intel x 86 - Assembly Language

AX = AH,AL - accumulatorBX = BH,BL - base registerCX = CH,CL - counter DX = DH,DL - data registerEAX - 32 bit version of the accumulatorEBX - 32 bit version of the base registerECX - 32 bit version of the counterEDX - 32 bit version of the data registerSI - source index registerDI - destination index register

BP - base pointer (used to access the stack)SP - stack pointerIP - instruction pointer (points to next instruction)ZF - zero flagCF - carry flagOF - overflow flagSF - sign flag (0 = non-negative, 1 = negative)IF - interrupt flag (0 = reset, 1 = set)

Registers

Page 4: Computing Machinery Intel x 86 - Assembly Language

Bit Label Desciption .0 CF Carry flag2 PF Parity flag4 AF Auxiliary carry flag6 ZF Zero flag7 SF Sign flag8 TF Trap flag9 IF Interrupt enable flag10 DF Direction flag11 OF Overflow flag12-13 IOPL I/O Priviledge level14 NT Nested task flag16 RF Resume flag17 VM Virtual 8086 mode flag18 AC Alignment check flag (486+)19 VIF Virtual interrupt flag20 VIP Virtual interrupt pending flag21 ID ID flag

The EFLAGS register holds the operating state of the processor. These values are changed by the execution of machine language instruction. Each bit gives the state of a specific parameter relative to the last instruction executed.

Page 5: Computing Machinery Intel x 86 - Assembly Language

"Secret" Registers

Some of the registers provided on the x86 (since the 386) by Intel are not well documented. These registers include control registers, debug registers, test registers and protected mode segmentation registers. The Control Registers are labeled CR0 through CR4. The Debug Registers are DR0 through DR7. The Test Registers (removed from the Pentium) are labeled TR3 through TR7. There are also protected mode segmentation registers, called the Global Descriptior Table Register (GDTR), the Interrupt Descriptor Table Register (IDTR), the Local DTR (LDTR) and the Table Register (TR).

Page 6: Computing Machinery Intel x 86 - Assembly Language

Step 1: Open an MS-DOS prompt window, go to the directory containing the DEBUG program (this could be C:\windows\command). For those of you reared on Microsoft Windows Look in the Programs directory under Startf ro MS-DOS Prompt. If you are using an old version of Windows you should find the DEBUG utility in the c:\windows\command directory. From a DOS prompt, enter,

cd c:\windows\command or whereever the DEBUG utility is found.

Running a Simple Assembly Language Program

For Windows 2000 you may need to go to the c:\winnt\system32 directory. You can obtain a command console on a Windows XP machine by running CMD from the RUN utility under START.

From the command console (once you are in the correct directory), run the DEBUG utility by typing its name at the prompt and pressing the <Enter> key.

Page 7: Computing Machinery Intel x 86 - Assembly Language

Step 2: You should see a dash on the screen. Now enter

-A100

(the dash - is already on the screen, so you don't have to enter it). This tells your program to begin at address 100 hex. All .COM files are loaded at address 100 hex. You should see two groups of four digits (hex) separated by a colon. This is the actual address at which you will be entering your assembly program.

Page 8: Computing Machinery Intel x 86 - Assembly Language

Step 3: Enter the following program (press enter at the end of each line)

MOV DL, 24 MOV AH, 2 INT 21 INT 20 Now press an extra carriage return to return to the dash prompt.

Page 9: Computing Machinery Intel x 86 - Assembly Language

Step 4: Now enter

G106

This will run the program and through address ...106 and then display the contents of the CPU registers.

Page 10: Computing Machinery Intel x 86 - Assembly Language

Step 5: Run the program again but this time just enter -G. The program runs and then terminates normally. In order to see the contents of the registers, we need to use the -R command. However, since the program was permitted to complete the registers are automatically cleared.

To see the program stored in memory, enter -d100 which will display the contents of memory starting at address ...100. To quit DEBUG, enter Q.

Page 11: Computing Machinery Intel x 86 - Assembly Language

Viewing Assembly Code

You can view any portion of memory using the U command.  To view a page of the contents of memory at a particular location simply enter the command followed by the starting address

-U100

Page 12: Computing Machinery Intel x 86 - Assembly Language

You can also specify a range of addresses by included the starting and ending address separated by commas.

-U 1000, 1010

Page 13: Computing Machinery Intel x 86 - Assembly Language

Printing Messages to the Screen

If you wanted to print messages to the screen it would be rather cumbersome to print one character at a time. Instead we can Set AH=9 to instruct the DOS interrupt INT 21 to print an entire string.

100 MOV AH,9 102 MOV DX,109 105 INT 21 107 INT 20 109 DB 'This is a sample string.$'

Enter and run this program.  If we view the source code using the U command the string is interpreted as a sequence of assembly lanauge instructions.  It is sometimes preferred to use the D command and view the code as data.

Page 14: Computing Machinery Intel x 86 - Assembly Language

You can invoke a carriage return using the ASCII value 10 (A hex) and a linefeed using the ASCII value 13 (D) as shown below,

100 MOV AH,9 102 MOV DX,109 105 INT 21 107 INT 20 109 DB 'This is',A,D,'a sample string.$'

Page 15: Computing Machinery Intel x 86 - Assembly Language

Indirect Addressing Mode

To place the number 9 hex into offset address 200 hex, we load the address 200h into BX, load the value 9 into AL and the transfer the value AL into the memory location pointed to by BX.

MOV BX, 200 MOV AL, 09 MOV [BX], AL INT 20

We can check to see if the value 9 was placed at offset address 200 by calling the command -D200.

Page 16: Computing Machinery Intel x 86 - Assembly Language

Saving Programs to Disk

The DEBUG program can be saved to disk by following the procedure outlined below: 1. Set the BX register to 0.

2. Set the CX register to the number of bytes contained in the program.

3. Use the N command to name the program.

4. Use the W command to write the program to disk.

The BX and CX registers can be set to any value by using the R command. Typing the command RBX causes the DEBUG utility to display the contents of this register and wait for you to enter a new value. To keep the present value just press the <Enter> key. Otherwise enter the new value. In this case you will want to enter a 0 value.

Page 17: Computing Machinery Intel x 86 - Assembly Language

Next set the CX register to the number of bytes contained in the program being saved. You can set this register to a value that is larger than the program size but this is not recommended.

Now set the name of the program to be saved using the N command

-Ntest.com

You can save .COM files to the A: drive by including the drive letter as part of the file name.

-NA:test.com

Finally you can write the file to the designated drive by using the W command. To retrieve the saved program set the name and then enter L (load command).

Page 18: Computing Machinery Intel x 86 - Assembly Language

Interacting with the .COM Program During Execution

Write and run the mystery .COM program below and then use the <Cntrl><Break> command to interrupt it. For Windows 2000 you can stop the program with a <Cntrl> C. If you touch any other key you may have to close the console window to interrupt this program.

100 MOV AH,2 102 MOV DL,24 104 INT 21 106 JMP 104

Before restarting the program you may want to reset the instruction pointer register (program counter) to 100 using the command,

-RIP100

You can specify a breakpoint to stop the program by including the breakpoint address following the G command.

-G 102

When the breakpoint is reached the values of the registers are displayed to support diagnostics. Setting breakpoints is a good way to view the contents of the registers before they are reset on exit.

Page 19: Computing Machinery Intel x 86 - Assembly Language

MOV AL,13    copies 13 to AL MOV AX,BX    copies BX to AX MOV DL,CL    copies CL to DL MOV BL,[BX]  copies the contents of the memory at address BX to BL

MOV Instruction

Transfers a byte or word from source to destination.  The source can be a register, an immediate number, or an address.  The destination can be a register or an address.

Page 20: Computing Machinery Intel x 86 - Assembly Language

Transfers program to a procedure specified by the interrupt number.  The interrupt returns processing to the address after the INT instruction.

INT 21h   executes interrupt 21 hex (DOS interrupt) INT 20h   executes interrupt 20 hex (.COM exit interrupt) INT 16h   executes interrupt 16 hex (keyboard interrupt) INT 10h   executes interrupt 10 hex (screen interrupt)

INT Instruction

Page 21: Computing Machinery Intel x 86 - Assembly Language

Transfers program to address

JMP 1234     jumps to offset address 1234 JMP 12:34    jumps to segment 12, offset 34

JMP Instruction

Page 22: Computing Machinery Intel x 86 - Assembly Language

Decements CX, and then transfers program to address if CX>0. If CX=0 transfers program to next instruction

LOOP 1234    loops to offset address 1234

LOOP Instruction

Page 23: Computing Machinery Intel x 86 - Assembly Language

Increments a register by 1.

MOV CL, 42 INC CL       CL now equals 43

INC Instruction

Page 24: Computing Machinery Intel x 86 - Assembly Language

Decrements a register by 1. MOV CL, 42 INC CL       CL now equals 41

DEC Instruction

Page 25: Computing Machinery Intel x 86 - Assembly Language

Transfers a byte or word from an external port to AX.  If the port value is greater than FF hex, then it must be placed in register DX.

IN AL, 13      sends contents of 8-bit port 13 to AL IN AX, B3      sends contents of 16-bit port B3 to AX MOV DX,FB00  IN AL,DX       sends contents of 8-bit port FB00 to AL

IN Instruction

Page 26: Computing Machinery Intel x 86 - Assembly Language

Transfers a byte or work from AL or AX to an external port.  If the port value is greater than FF hex, then it must first be placed in register DX.

OUT 13, AL     sends contents of AL to 8-bit port 13 OUT B3, AX     sends contents of AX to 16-bit port B3 MOV DX,FB00 OUT DX, AL     sends contents of AL to 8-bit port FB00

OUT Instruction

Page 27: Computing Machinery Intel x 86 - Assembly Language

Exchanges the contents of two 8 or 16-bit registers.

XCHG AL, DH XCHG AX,BX

XCHG Instruction

Page 28: Computing Machinery Intel x 86 - Assembly Language

Compares a register or an immediate byte or word with a register by performing a SUB operation.  The flag register is set based on the result.  The result is discarded.

CMP AL, 13     zero flag set if AL=13 CMP AX, BX     zero flag set if AX=BX

CMP Instruction

Page 29: Computing Machinery Intel x 86 - Assembly Language

Tests a register or an immediate byte or word with a register by performing an AND opera;tion.  The flag register is set based on the result.  The result is discarded.

TEST AL, 13    zero flag set if AL=13 TEST AX, BX    zero flag set if AX=BX

TEST Instruction

Page 30: Computing Machinery Intel x 86 - Assembly Language

Jumps to an address if a CMP instruction produced a destination equal to a source.

CMP AX, 13  JG 1234       jumps to address 1234 if AX>13 JE 1234       jumps to address 1234 if AX=13 JNE 1234      jumps to address 1234 if AX/=13 JL 1234       jumps to address 1234 if AX<13 JGE 1234      jumps to address 1234 if AX>=13 JLE 1234      jumps to address 1234 if AX<=13

'JUMP' Instructions

Page 31: Computing Machinery Intel x 86 - Assembly Language

Rotates the register right by 1 bit.  The LSB wraps over to the MSB.  If more than 1 bit needs to be rotated, set the number of bits to rotate in the CL register first

MOV AX,0083  this is 0000 0000 1000 0011 ROR AX,1     now AX= 1000 0000 0100 0001

MOV AL,83    this is 1000 0011 MOV CL,2 ROR AL,CL    now AL= 1110 0000

ROR Instruction

Page 32: Computing Machinery Intel x 86 - Assembly Language

Rotates the register left by 1 bit.  The MSB wraps over to the LSB.  If more than 1 bit needs to be rotated, set the number of bits to rotate in the CL register first.

MOV AL,83     sets AX = 0000 0000 1000 0011 ROL AX,1      now  AX = 0000 0001 0000 0110

MOV AX,0083   sets AX = 0000 0000 1000 0011 MOV CL,2 ROL AX,CL     now  AX = 0000 0010 0000 1100

ROL Instruction

Page 33: Computing Machinery Intel x 86 - Assembly Language

Shifts the register right by 1 bit.  The LSB is lost, the MSB is loaded with a 0 bit.

MOV AX,F002   sets AX = 1111 0000 0000 0010 SHR AX,1      sets AX = 0111 1000 0000 0001

MOV AX,F002   sets AX = 1111 0000 0000 0010 MOV CL,2 SHR AX,CL     sets AX = 0011 1100 0000 0000

SHR Instruction

Page 34: Computing Machinery Intel x 86 - Assembly Language

Shifts the register left by 1 bit.  The MSB is lost, the LSB is loaded with a 0 bit.

MOV AX,0083   sets AX = 0000 0000 1000 0011 SHL AX,1      sets AX = 0000 0001 0000 0110

MOV AX,0083   sets AX = 0000 0000 1000 0011 MOV CL,2 SHL AL,CL     sets AX = 0000 0000 0000 1100

SHL Instruction

Page 35: Computing Machinery Intel x 86 - Assembly Language

Performs a Boolean AND operation.  The result is stored in the destination register.

MOV AX,1234   sets AX = 0001 0010 0011 0100 MOV BX,ABCD   sets BX = 1010 1011 1100 1101 AND AX,BX     sets AX = 0000 0010 0000 0100

AND Instruction

Page 36: Computing Machinery Intel x 86 - Assembly Language

Perfoms a Boolean OR operation.  The result is stored in the destination register.

MOV AX,1234   sets AX = 0001 0010 0011 0100 MOV BX,ABCD   sets BX = 1010 1011 1100 1101 OR  BX,AX     sets BX = 1011 1011 1111 1101

OR Instruction

Page 37: Computing Machinery Intel x 86 - Assembly Language

Performs a Boolean Exclusive OR operation.  The result is stored in the destination register.

MOV AX,1234   sets AX = 0001 0010 0011 0100 MOV BX,ABCD   sets BX = 1010 1011 1100 1101 XOR AX,BX     sets AX = 1011 1001 1111 1001

XOR Instruction

Page 38: Computing Machinery Intel x 86 - Assembly Language

Performs a Boolean NOT operation on the specified register (one's complement)

MOV AX,1234   sets AX = 0001 0010 0011 0100 NOT AX        sets BX = 1110 1101 1100 1011

NOT Instruction

Page 39: Computing Machinery Intel x 86 - Assembly Language

Adds a source to a destination.  The results is stored in the destination register

ADD AX,13     replaces AX with 13 added to AX ADD AL,DL     replaces AL with AL added to DL

ADD Instruction

Page 40: Computing Machinery Intel x 86 - Assembly Language

Subtracts a source from a destination.  The result is stored in the destination register.

SUB AX,13    replaces AX with AX minus 13 SUB AL,DL    replaces AL with AL minus DL

SUB Instruction

Page 41: Computing Machinery Intel x 86 - Assembly Language

(byte version) Multiplies AL by a source value.  The source can be a register or an address The result will be stored in AX. (word version) Multiplis AX by a source value.  The source can be a register or an address.  The high 16-bit result is stored in DX, the low 16-bit result is stored in AX.

MUL DL      multiplies AX by DL result is in AX MUL BX      multiplies AX by BX result is in DX,AX

MUL Instruction

Page 42: Computing Machinery Intel x 86 - Assembly Language

(byte version) Divides AX by an 8-bit source value.  The source can be a register or an address.  The result is stored in AX.  The quotient is in AL, and the remainder is in AH. (word version) Divides AX by a 16-bit source value.  The source can be a register or an address.  The quotient is placed in AX, and the remainder is placed in DX.

DIV DL     divides AX by DL quotient is in AL,                             remainder is in AH DIV BX     divides AX by BX quotient is in AX,                             remainder is in DX

DIV Instruction

Page 43: Computing Machinery Intel x 86 - Assembly Language

I/O PortsA port that is common to all Intel based PCs is the parallel (printer) port.  Many PC's have more than one printer port, labeled LPT1, LPT2 and LPT3 for example.  The address for these ports is not the same for all computers, but we can determine these addresses for a particular computer by checking the values at memory segment 40hex, beginning at offset 8. From the DEBUG utility enter the command,

D40:8

Page 44: Computing Machinery Intel x 86 - Assembly Language

The address for LPT1 is therefore 378hex (high byte follows low byte)  the next 4 bytes are 00 00 00 00 so there is only one printer port on this system.

We can check to see of the printer (dot-matrix only) is working and ready to print.  We can test the ACK pin for a value of 1 to verify that the printer is on and there are no errors.  The pin will equal 0 if the printer is off or if there is an error.  The ACK is bit 5 of port 2.

100    MOV DX, 378 103    IN  AL,DX 104    INT 20

Page 45: Computing Machinery Intel x 86 - Assembly Language

Since AL=04hex (0000 0100 binary) we see that bit 5 is zero. Let's look at a short assembly program to display the contents of a register DL to the screen in hexidecimal symbols (i.e. ASCII characters).

MOV DL,A9  ;loads DL with the value A9 (1010 1001) MOV DH,DL  ;copies DL into DH MOV CL,4   ;will be used to shift DL 4 bits to the right SHR DL,CL  ;DL now contains the upper nibble (0000 1010) ADD DL,30  ;adds 30hex to DL (ASCII value) (0011 1010) CMP DL,39  ;if symbol is >9 we need to add 7 this skips over            ;the symbols between 0..9 and A..Z in ASCII table JLE 113 ADD DL,7 MOV AH,02  ;this is offset address 113 INT 21     ;DOS interrupt to print to screen MOV DL,DH  ;recover original value in DL AND DL,0F  ;leaves lower nibble of DL ADD DL,30  ;converts to ASCII value CMP DL,39  ;same test as on previous symbol JLE 127 ADD DL,7 MOV AH,02  ;this is offset address 127 INT 21     ;DOS interrupt to print to screen INT 20     ;terminates program

Whew!  All this to display two characters to the screen. It doesn't get any more exciting than this...unfortunately.

Page 46: Computing Machinery Intel x 86 - Assembly Language

"I could sit in an insurance seminar for days with a big

smile on my face.. and they would ask me, 'How can you

stand it?' I would say, 'Because I've programmed in

assembly, I can take anything!' "

-- bonus point for original context of this paraphrase.

Page 47: Computing Machinery Intel x 86 - Assembly Language