Concise x86 assembly Language Tutorial (4)

Source: Internet
Author: User
Tags error code valid

Chapter III Operating memory

In the previous chapters, we've learned the basics of how to use registers. As mentioned at the end, it doesn't make much sense to just use registers to do a little arithmetic, since they can't save too much data, so for programmers, he definitely wants to access memory to save more data.

I'll explain separately how to manipulate memory in both protected and real mode, but before we do that, let's familiarize ourselves with the structure of memory in both of these modes.

3.1 Real Mode

In fact, in real mode, memory is more confusing than the structure in protected mode. Memory is split into segments, and when manipulating memory, you need to specify segments and offsets. However, it is very easy to understand these concepts. Take a look at the figure below:

Segment-register this pattern is a scar left by early hardware circuit limitations. The address bus was 20-bit at the time.

However, 20-bit's address cannot be placed in 16-bit registers, which means that 4-bit must be placed elsewhere. Therefore, in order to access all memory, two 16-bit registers must be used.

This design compromise led to today's segment-offset pattern. In the original design, one of the registers was only 4-bit valid, however, in order to simplify the program, two registers were 16-bit valid, and the weights were calculated and the 20-bit addresses were identified at the time of execution.

The offset is 16-bit, so one segment is 64KB. The following figure can help you understand how the 20-bit address is formed:

The address of a segment-offset identification is usually the form of a segment: offset.

Because of such a structure, one memory has multiple corresponding addresses. For example, 0000:0010 and 0001:0000 refer to the same memory address. Another example,

0000:1234 = 0123:0004 = 0120:0034 = 0100:0234
0001:1234 = 0124:0004 = 0120:0044 = 0100:0244

As one of the negative effects, adding 1 to the paragraph is equivalent to adding 16 to the offset, rather than a "new" segment. Conversely, adding 16 to the offset and adding 1 to the paragraph is equivalent. At some point, the "granularity" of a segment is considered to be 16 bytes.

Exercises
Try converting the following address to a 20bit address:

2ea8:d678 26cf:8d5f 453a:cfad 2933:31a6 5924:DCCF
694e:175a 2b3c:d218 728f:6578 68E1:A7DC 57ec:aeea

Slightly higher requirements are, write a program will be ax, offset to the address of BX to 20bit address, and stored in the EAX.

[Answer to the problem above]

We can now write a real program.

Classic program: Hello, World

;;; You should get a 29-byte. com file
. MODEL TINY
. CODE
CR equ 13
LF equ 10
Terminator Equ ' $ '
ORG 100h
Main PROC
MOV Dx,offset smessage
MOV ah,9
int 21h
MOV ax,4c00h
int 21h
Main ENDP
Smessage:
DB ' Hello, world! '
DB Cr,lf,terminator
End Main

; . The memory model for COM files is ' TINY '
; Start of code snippet
; Enter
; Line Wrap
; DOS string Terminator
; Code start address is cs:0100h
; Make DS:DX point to Message
; int 21h (DOS interrupt) feature 9-
; Display string to standard output device
; int 21h function 4CH-
; Terminates the program and returns an error code for AL
 

; Specifies that the entry point is main at the end of the program

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.