What is the purpose of CS and IP registers in Intel 8086 assembly?

帅比萌擦擦* 提交于 2019-11-27 19:51:54

问题


So, as the question states, what is the purpose of CS and IP registers in intel's 8086

I found this explanation:

Code segment (CS) is a 16-bit register containing address of 64 KB segment with processor instructions. The processor uses CS segment for all accesses to instructions referenced by instruction pointer (IP) register. CS register cannot be changed directly. The CS register is automatically updated during far jump, far call and far return instructions.

and this for IP:

Instruction Pointer (IP) is a 16-bit register.

I don't really understand what this basically means, so if someone could provide a more "vivid" explanation, that would be great :)


回答1:


Since the Instruction Pointer (IP) is 16 bit it means you can only have 64k instructions (2^16), which wasn't much even in the 80s. So to expand the address space you have a second register which addresses 64k blocks. You could consider cs:ip together as one 32 bit register which is then capable of addressing 2^32 bytes...ie 4G which is what you get on a processor which uses 32 bit addresses. The 8086 was using 20 bits of addresses, so you could access 1M of memory.




回答2:


The physical address is calculated from 2 parts. i) segment address. ii) offset address. The CS(code segment register) is used to address the code segment of the memory i.e a location in the memory where the code is stored. The IP(Instruction pointer) contains the offset within the code segment of the memory. Hence CS:IP is used to point to the location (i.e to calculate the physical address)of the code in the memory.




回答3:


The instruction that will be executed next is that at memory address equal to:

16 * CS + IP

This allows 20 bits of memory to be addressed, despite registers being only 16 bits wide (and it also creates two distinct ways to encode most of the addresses).

The effect of CS is analogous to that of the other segment registers. E.g., DS increments data accesses (that don't specify another segment register) by 16 * DS.

CS

The instructions that modify CS are:

  • ljmp (far jump)
  • lcall (far call), which pushes ip and cs to the stack, and then far jumps
  • lref (far return), which inverses the far call
  • int, which reads IP / CS from the Interrupt Vector Table
  • iret, which reverse an int

CS cannot me modified by mov like the other segment registers. Trying to encode it with the standard identifier for CS, which GNU GAS 2.24 does without complaining if you write:

mov %ax, %cs

leads to an invalid code exception when executed.

To observe the effect of CS, try adding the following to a boot sector and running it in QEMU as explained here https://stackoverflow.com/a/32483545/895245

/* $1 is the new CS, $1f the new IP. */
ljmp $1, $after1
after1:
/* Skip 16 bytes to make up for the CS == 1. */
.skip 0x10
mov %cs, %ax
/* cs == 1 */

ljmp $2, $after2
after2:
.skip 0x20
mov %cs, %ax
/* cs == 2 */

IP

IP increases automatically whenever an instruction is executed by the length of the encoding of that instruction: this is why the program moves forward!

IP is modified by the same instructions that modify CS, and by the non-far versions of those instructions as well (more common case).

IP cannot be observed directly, so it is harder to play with it. Check this question for alternatives: Reading Program Counter directly




回答4:


Once you write .code in your assembly program text, that .code points to the cs value. any command later or earlier in the file will be addressed as per cs:ip , where ip is an offset value of from cs.

Of course, you have to bear in mind that assembly compiler will convert the text into machine code instructions first.




回答5:


since the 8086 processor uses 20 bits addressing, we can access 1MB of memory, but registers of 8086 is only 16 bits,so to access the data from the memory we are combining the values present in code segment registers and instruction pointer registers to generate a physical address, it is done by moving the value of CS 4 bits towards left and then adding it with the value IP

EXAMPLE:

value of CS is 1234Hex(hexa decimal)

value of IP is 5678Hex

now value of CS after moving 4 bits left is 12340Hex then after adding with IP value it is 179B8Hex which is the physical address




回答6:


IP register - IP is Instruction Pointer. Its function is the same as PC (program counter) in other microprocessor which is to point to the next instruction to be fetched by BIU unit to be feed into EU unit.



来源:https://stackoverflow.com/questions/17777146/what-is-the-purpose-of-cs-and-ip-registers-in-intel-8086-assembly

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!