16-bit assembly language second speaking system invocation principle, as well as various registers detailed

Source: Internet
Author: User

The assembly code was simply written yesterday, and the first HelloWorld displayed to the screen was executed.

Problem?

How did the HelloWorld show up?

A video card, the concept of video memory

1. Show Hello to operate the display, which is very primitive, at that time the programmer, and there is no such as RGB (red and green blue) such a three-color true color, at that time is the operation of the video card, defines a standard

The standard is that we're going to write data to a fixed address and it will show up.

Specific process

Operating graphics, graphics card with its own cache, write data to memory,-> Display data (displayed on the screen)

But at that time there is no word, so it began to make, that is, the English 26 of the English alphabet made to the color is the same

Like we build a 1, and we put it in memory.

0 0 1) 0 0

0 0 1 0 0 Consider this 1 as a two-dimensional array, and put the data in the video memory to show 1.

0 0 1) 0 0

Two, the concept of the Sawtooth

We have found that many games have an option called pit-serrated

In fact, the sawtooth is because, before doing 1, the color is black and white such words have a little bit of tooth marks. Look uncomfortable, so behind, with three colors, and a word of the same color are put together

So the words look very smooth, very good-looking.

As can be seen, I wrote a 1 words, magnified next to the like blue, red yellow and so on, and the previous is a black one, so look hard to see.

Three, system call principle

For example, we want to display a string, the direct "Hello" double quotation marks are included, to the operating system. It will be displayed.

So what is the principle.

In the first lecture, I said a dictionary of instructions, there is a function number int 21 means we want to call that item, and the hardware is to provide a table, call the time to look directly at the table

This table is an array of function pointers that can be called directly.

For example

But we will think that the operating system will call this 21st display data, then this table may be infinite, and the operating system provides tens of thousands of of the API, why a table enough?

So there is an AH register, the above diagram can be changed to

Of course, you may not need to switch this inefficient syntax, will do optimization, but the principle is this, the hardware manufacturers only provide instructions, that is, my CPU will call int 21 specified, find the 21st item in the array

This table is provided by the operating system.

So now you know what is int 21, and why AH gives 9 to display the string.

Iv. new Problem The string is displayed before the system is started

We have not found that the system has not been started before, it will show a string, this string is displayed through the motherboard BIOS

The BIOS is not system-dependent and takes precedence over system presence.

After the operating system is started, you can also call

In the instruction dictionary

In the interrupt code there will be said, the number of calls int, the parameter through what register to give. And so on. He is not the same table as the CPU, but it all exists before the operating system starts.

Five, register detailed 1. IP instruction Register

IP registers, as mentioned above, IP registers are suitable for use with CS segment registers, IP is offset (IP register is called instruction Register) he is representing the position of the next instruction to be determined by the offset of the CS segment Register + IP

For example, we have a code for assembly.

The execution location of the next instruction is 0100 IP equals 0100 and can be viewed with the P command. In fact, IP equals 0100 is the IP offset is 0100, with CS segment register * + IP offset, is equal to the actual physical address (that is, the location of the next specified instruction) is MOV ax,1

P Instruction Debug View

First time

The offset of the first instruction position is 0103 that is, MOV bx,2 will start execution, the following analogy (do not understand the paragraph register, the following fine)

2.Flag Flag Register

A good blog about the sign register connection https://my.oschina.net/clownfish/blog/142328

The flag flag registers the flag flag is 16-bit.

15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
Of Df IF Tf SF Zf Af Pf Cf

Usually the marker is 9, commonly used there are 6, not commonly used there are three, say not commonly used DF IF TF, the rest is commonly used

CF Carry Flag (Carry flag): When we do addition, for example, if the sum of two numbers is rounded up.

When carrying, the flag is 1, otherwise the flag is 0, if it is an addition operation, then the flag bit 1 for the carry, if the subtraction, then the flag is borrow

For example 3AH + 7CH = b6h (h in the Assembly represents the front of the number is 16 binary) No carry CF = 0

AAH + 7CH = (1) 26H, highest bit rounded, CF = 1

ZF 0 Flag (zero flag): Indicates that the result of your operation is 0, then ZF = 1, if not zero, ZF = 0

Example:

3AH + 7CH = b6h, result not zero, ZF = 0

84H + 7CH = (1) 00H, the result is 0, because the rounding, then CF = 1

SF symbol flag (sign flag):

Represents the result of the operation, the highest bit is 1, then SF is 1, otherwise the highest bit is not 1 SF = 0

3AH + 7CH = b6h, the highest bit of the result is not zero, it is 1 0 1 1 The highest bit is 1 so SF = 1 is considered a binary

84H + 7CH = (1) 00H, the result is 0, the highest bit is 0

PF parity Mark Bit (Parity flag)

In the result of your operation, the number of statistics 1 appears (binary statistics) if the number of 1 is zero, or even, then PF = 1, odd is 0

Like what

3AH + 7CH = b6h, = 1011 0110 B (suffix B stands for binary meaning) statistics, five 1 PF = 0 are present;

of overflow flag bit (OverFlow flag)

If the result of the operation is overflow (or the result is incorrect) then of = 1, otherwise of = 1

Like what

3AH + 7CH = b6h generated overflow, of = 1

AAH + 7CH = (1) 26H, rounded, but no overflow, of = 0

1. Overflow

When an integer of 8-bit expression is returned, +127--128

Within the range of 16-bit expressions: +32767~-32768

For example 3AH + 7CH = b6h, 3 A = 10 in eight-bit range 7C = 124 decimal

So the result is 58 + 124 = 182, far more than the range of 128, so produced a out, and on the one hand

The result of the b6h is a complement, and the value is-74, and obviously the result is incorrect.

2. Differences in overflow and rounding

Overflow flag of of, and carry Mark CF is different,

The extreme flag indicates whether the demerit of unsigned number operation is out of range, and the result of operation is still correct.

The overflow flag indicates whether the operation result of the signed number is out of range, and the result of the operation is already incorrect.

3. How to apply overflow and rounding

This depends on the programmer

When the processor operates on two operands, the result is evaluated according to the unsigned data (why unsigned, because the negative number has a complement)

and set the carry flag bit CF, colleague, based on whether the number of symbols beyond the range set a flag of, that is also set the carry, also set the overflow flag bit, set the overflow flag bit because the unsigned number has exceeded the range.

Using that sign depends on the programmer's own decision.

4. The principle of overflow judgment

In short, a positive plus positive number equals a positive number, but at this point the binary maximum of the result is 1 (1 is the meaning of a negative number) so the computer thinks it has produced a

AF auxiliary carry flag (auxiliary Carry flag)

The secondary flag bit mainly represents the lower 4 carry or borrow, and CF is different, CF is eight bits produce carry and borrow to set the flag bit

So this is the auxiliary carry flag bit

Like what:

3AH + 7CH = b6h, low four bit with carry, then AF = 1

DF direction flag bit (Direction flag)

For example, if our Si and Di address registers are to be memcpy, the memory will increase or decrease.

DF is the direction of the change in the control address.

DF = 0, the memory address is automatically incremented

DF = 1, the address of the memory is automatically reduced

The CLD instruction in the assembly, which indicates the reset direction flag, so that its df = 0, the address automatically increases

The STD directive in the assembly, which indicates the set direction flag, DF = 1, means that the address Zengdong is reduced.

IF interrupt Allow flag (interrupt-enable flag)

Official language: Used to control whether an externally masked interrupt can be responded to by the processor

Self-understanding

For example, the keyboard press, how to know the press, the past is an infinite loop, but the efficiency is particularly low, now changed to a keyboard press will be like the CPU to send a signal

When the CPU is executing the instruction, you press the keyboard, will give up the current instruction, to carry out the keyboard sent over the key command, but if we keep pressing the keyboard, is not the current instruction can not be executed, so we set the flag to block the current sent over the command

Assembly Instruction CLI Command reset interrupt flag if = 0;

Assembly instruction STI command set to interrupt flag: IF = 1

if = 1, it means that we can allow interrupts (that is, shielding instructions)

if = 0, if interrupt is forbidden

TF Trap Flag (TRAP flag)

Used to control the processor into a single-step operation (typically used by the debugger)

TF = 0, the processor is working properly

TF = 1, processor stepping instruction

Using this flag, the program can be debugged by instructions.

One-step debugging is the way to debug the program.

No assembly instruction, if set, then bitwise operation | On.

Second, segment registers and memory 1. Memory and Segment Registers

Nutshell

Is that the register is the internal memory of the CPU

Memory can hold external data

The hard drive can store external data, and it will still exist after power loss

2. Unit of expression of the data

Binary bit 0 1 composition

Bytes byte 8 bit bits consisting of

Word Word 16-bit: 2 bytes made up of

Double-character DWORD 32-bit, two-word composition

Divided into big-endian mode storage, and end-mode storage

The official language is LSB,MSB and so on. Small tail way, and big tail way

Big-endian mode: Low down address, high position to high address

Low address---------high address (e.g. Storage 1 2 3 4)

0x1 0x2 0x3 0x4

Small terminal mode

Low address, high level low address

Low address---------high address (e.g. Storage 1 2 3 4)

0x4 0x3 0x2 0x1

3. Storage unit and storage content

Each storage unit has an address number, called the memory address, in the C language is actually the memory address

Each storage unit holds the contents of one byte.

If the content is taken

[Address] = The value taken out, so the brackets in the array in the C language come in this way.

4. Solving CP's addressing capability

16-bit processor, can handle the largest data range is 2^16 data, that is, 64k, even if you install a 1MB of memory can not access

Solve:

1.8086CPU has 20 address lines, the largest addressable control is 2 ^20, the power address from 00000H-FFFFFH

2.8086CPU 1MB space is divided into a number of logical segments (Segment)

The maximum limit for each segment is 64KB, why, because the register is 16 bits, and no secondary address is 2 ^16

The low four bits of the short address are 0000b, why, because add 4 address bus, that is, 4 more, so give 4

In this way, a storage unit, in addition to having a unique physical address, has a number of logical addresses

Now in order to solve this problem of addressing, so with 2 register storage, that is why the above said CS and IP together to determine a physical memory execution

The next instruction.

And there are many logical addresses, divided into several paragraphs, that is, the segment address, the use of segment base address: Offset address in the paragraph so that storage

Segment Address: The address of the segment is the starting position of the logical address in main memory

8086 the address of the specified paragraph must be%16, then the address is xxxx0h, because it is 16 binary, so the last 0, converted into binary is the back 4 binary 0

Because of the%16 address, it is now possible to use the 16-bit segment register to represent the segment address.

Offset address

Offset address indicates the offset of the primary cell distance segment address starting position

Each segment is also not more than 64KB, can also be stored with registers, so the IP appears

Conversion of physical address and logical address

The logical address (segment address) is shifted to the Left 4 bits (that is, *2^4) plus the offset address, the physical address of the 20-bit is obtained

A physical address can have multiple logical addresses

Like what

Logical address 1460:100 Physical address is 14600 (because *16) + upper offset = 14700H

1380:f00 = 13800 + F00 = 14700H

Segment registers commonly used in 8086

CS (Code snippet) specifies the starting address of the code snippet

SS (stack segment) indicates the starting address of the cut

DS (data segment) indicates the starting address of the data segment

ES (additional segment) indicates the starting address of the additional end

And these are called Memory 4 in the C language.

Why segment:

We wrote the last one to show HelloWorld and write to the file, now the compiled code and data are together, but once the program is larger, it is not easy to get.

Job: Use the debug compiler, and use the instructions to see what the status of the flag bit is.

Second talk of homework and tools get connections

Link: Http://pan.baidu.com/s/1mi3KW1U Password: 0u0e

16-bit assembly language second speaking system invocation principle, as well as various registers detailed

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.