The benefits of separating data segments and code segments in assembly

Publisher:chi32Latest update time:2016-10-12 Source: eefocusKeywords:Assembly Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
Is there any benefit in defining the data segment and code segment in the same segment in assembly?

In assembly language, the code segment and data segment are placed in the same segment. Are there any requirements for the position of the variable when defining a pseudo instruction? Is there any difference if it is placed after Start: or before Start? Does it matter if the macro definition or subroutine is placed before or after Start: (the label of the program start)?

The advantage is that it can save a few bytes of the compiled program, and it can also save you a few words when writing source code, because you don't need to make a data segment. However, when writing large programs, it is best to separate the data segment and the code segment, because that is clearer. Of course, if you think it is clearer to put data and code together, you can stick to your own personality.
Well, this location is very particular.
First: The data defined in the code segment cannot be defined where CS:IP may touch it, because once the variable is touched by CS:IP, the data will be executed as an instruction. The CPU will be very angry and the consequences will be serious. Example:
Correct definition:
code segment
var01 db 64
start:
 mov ax,cx
  mov ds,ax
  mov al,var01
  mov ax,4C00H
  int 21H
  ; You can also put "var01 db 64" in this line. If you put it here, the data defined above will not be used
  ; After the CPU executes int 21H, CS:IP will float to other places, so it will not be executed here
code ends
Error definition:
code segment
start:
 mov ax,cx
  mov ds,ax
var01 db 64; The CPU will visit here when executing, so it cannot be defined here
 mov al,var01
  mov ax,4C00H
  int 21H
code ends
Second: Data should be defined at the beginning or end of the code segment, so that part of the code segment is data and part is code. Otherwise, if you define data sometimes and code sometimes, people who read the source code will be very angry. But there may be exceptions to this rule, for example:
code segment
start:
 mov ax,cx
  mov ds,ax
  jmp OMG
var01 db 64 ; The previous instruction will make CS:IP point to the following instruction
 If you think this is beautiful, you can do this.
OMG:
 mov al,var01
  mov ax,4C00H
  int 21H
code ends
I don't know where the macro definition is defined, but it seems to be defined at the beginning of the program.
The subroutine is defined before start or
mov ax,4C00H
int 21H ;(that is, program return code)
The back is the same
You can also use the previous example to create a space that will not be executed directly, like this:
code segment
var01 db 64
start:
 mov ax,cx
  mov ds,ax
  jmp OMG1
OMG2:
 mov al,var01
  ret
OMG1:
 call OMG2
  mov ax,4C00H
  int 21H
code ends
I feel this approach is very 2-step, the subroutines should actually be put together, like this:
code segment
var01 db 64
start:
 mov ax,cx
  mov ds,ax
  call OMG1
  call OMG2
  mov ax,4C00H
  int 21H
OMG1:
 mov al,var01
  ret
OMG2:
 xor al,al
  ret
code ends
 
Code and data should actually be the same. The reason for separating them is that the CPU executes sequentially (except for jump instructions), and if there is data at the address, an error will occur. Also, if the amount of data is large, it is very convenient to operate, but it will be very troublesome to put them together. . . . .
Keywords:Assembly Reference address:The benefits of separating data segments and code segments in assembly

Previous article:Interrupt "interrupt X using X" detailed explanation
Next article:Assembly instruction full English name

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号