Chapter 4 Memory Data (X86 Assembly Tutorial)

Publisher:静逸心境Latest update time:2015-05-15 Source: 51hei Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
      Memory is where temporary data is stored. The running programs, the pictures you are viewing, the texts you are editing, and the tutorial you are reading now are all copied to the memory.
 
This chapter is about memory data. Nowadays, computers have over G of memory. But did you know that when the computer was first turned on, a maximum of 1MB of memory could be used! This is because the CPU actually has several modes, real mode, protected mode, and 64-bit mode (I won’t talk about the more powerful modes later, because... I don’t know either!) To access memory, addressing is required. When the computer was first turned on, the CPU was in real mode. What is real mode? That was the 16-bit mode of the CPU a long time ago. At that time, the computer had only 1M memory. B, that is to say, the addressing mode only needs to be able to address 1MB. Naturally, it is impossible to use all the G memory. Later, the 32-bit protected mode of 80386 CPU came out (in fact, there was an 80286 in the middle, and there was a protected mode since 286, but it seems not to be 32-bit), and the register of 386 protected mode is 32 bits. It can address 4GB of memory (for the time being, only the real mode is taught, and the protected mode and the like will not be explained in detail. I will see if it can be written into this tutorial later). Now the newly appeared 80686 has a 64-bit mode, and the register is 64 bits.
        Well, let's talk about the addressing method of real mode. Memory is one-dimensional, and data is arranged in a line. To access memory, you need to provide a memory address. The address of the first byte of memory is 0 (in computer mathematics, 0 is the starting point). Yes, that's it. The address of the second byte is 1. And so on. The addressing method is the way to provide an address to access memory. In real mode, registers have 16 bits, which means that a single register can only express an address of up to 64KB (please verify it yourself, the verification method is: use the binary mode of the calculator, enter 16 1s, and convert to decimal)
        64KB! That's obviously not enough! Therefore, real mode addressing uses 2 registers for addressing, and addresses in the "segment: offset" manner.
        As mentioned above, the segment registers CS, DS, ES, and SS are used for addressing and storing the data of the "segment". As for the data of the "offset" part... it can be represented by any of the registers BX, SI, DI, SP, IP, and BP, or directly by numbers. Example:
 
CS:BX
CS:1234
        So, how does "segment:offset" express a one-dimensional address? See below:
 
The CPU notifies the memory where it wants to operate through the address line. There are 20 address lines in the CPU, that is, the "segment: offset" addressing method can only address 20-bit addresses (so even if two 16-bit registers are combined, it is not possible to address a 32-bit memory address...), 20 bits, that is, 1MB. So, how to convert two 16-bit data into 20 bits?
The algorithm is as follows:
(segment*16)+offset
Well, the value in the segment register is multiplied by 16 (10 in hexadecimal) and added to the offset address.
Example:
Segment register = 15
Offset address = 128
So the memory address obtained is: (15*16)+128=368

 
        Well, we have finished talking about addressing mode. Next, we will talk about the order of low and high bits of data. In the future, you will encounter some situations where numbers are stored in reverse, for example, 53,217 is stored in memory as 71235. This is because the high and low bits are arranged in order. In this number, 7 is the ones, so it is at the front, 1 is the tens, so it is the second. And so on. Maybe you have been shocked to find that, yes, the numbers in the computer are not reversed, but the numbers we use in reality are reversed. We use 53217 to represent 53,217, and we put the highest 5 in the ten thousandth place at the front! The same is true in the register, the low bit is in the front and the high bit is in the back.

 
        Next, let's talk about the way numbers are represented in assembly. In assembly, you can enter numbers in decimal, binary, hexadecimal, etc. (Of course, after compiling and generating code, they are all in binary.) With so many bases, if there is no explanation, the assembly compiler will definitely make a mistake. So how do we tell the compiler which number is in which base?
        We can add an English letter after the number to represent it. For binary numbers, add B after it, for example: 01110011B. For hexadecimal numbers, add H after it, for example: 1F3AH. As for decimal numbers, it is not necessary. All numbers have an English letter after it, so it doesn't matter if one of them doesn't have an English letter, right? Because decimal numbers are the most commonly used, it is given the right not to have an English letter after it. In assembly, decimal numbers do not need to be represented specially.
        In addition, since hexadecimal involves English letters, ABCDEF, it may be confused with the assembly code, so if the first digit of the hexadecimal is an English letter, a 0 must be added in front.
        Example:
 
        A2H
 
        Must be written as:
 
        0A2H

 
Okay, that’s all for this chapter~
Reference address:Chapter 4 Memory Data (X86 Assembly Tutorial)

Previous article:Chapter 6 Hands-on Operation - Use of DEBUG 2 (X86 Assembly Tutorial)
Next article:Chapter 2 Some Data Units (X86 Assembly Tutorial)

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号