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. . . . .
Previous article:Interrupt "interrupt X using X" detailed explanation
Next article:Assembly instruction full English name
- Popular Resources
- Popular amplifiers
- Naxin Micro and Xinxian jointly launched the NS800RT series of real-time control MCUs
- How to learn embedded systems based on ARM platform
- Summary of jffs2_scan_eraseblock issues
- Application of SPCOMM Control in Serial Communication of Delphi7.0
- Using TComm component to realize serial communication in Delphi environment
- Bar chart code for embedded development practices
- Embedded Development Learning (10)
- Embedded Development Learning (8)
- Embedded Development Learning (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Intel promotes AI with multi-dimensional efforts in technology, application, and ecology
- ChinaJoy Qualcomm Snapdragon Theme Pavilion takes you to experience the new changes in digital entertainment in the 5G era
- Infineon's latest generation IGBT technology platform enables precise control of speed and position
- Two test methods for LED lighting life
- Don't Let Lightning Induced Surges Scare You
- Application of brushless motor controller ML4425/4426
- Easy identification of LED power supply quality
- World's first integrated photovoltaic solar system completed in Israel
- Sliding window mean filter for avr microcontroller AD conversion
- What does call mean in the detailed explanation of ABB robot programming instructions?
- Ultrasound patch can continuously and noninvasively monitor blood pressure
- Ultrasound patch can continuously and noninvasively monitor blood pressure
- Europe's three largest chip giants re-examine their supply chains
- Europe's three largest chip giants re-examine their supply chains
- Breaking through the intelligent competition, Changan Automobile opens the "God's perspective"
- The world's first fully digital chassis, looking forward to the debut of the U7 PHEV and EV versions
- Design of automotive LIN communication simulator based on Renesas MCU
- When will solid-state batteries become popular?
- Adding solid-state batteries, CATL wants to continue to be the "King of Ning"
- The agency predicts that my country's public electric vehicle charging piles will reach 3.6 million this year, accounting for nearly 70% of the world
- EEWORLD University - Learn PIC microcontroller in ten days
- Infineon's Home HVAC Solutions Invite You to Open Blind Box
- What is peak-to-peak value? Peak value? Effective value? Understanding these parameters makes amplifier selection so easy!
- Verilog basic question
- Smart Lock
- I need help designing a 100mA constant current source. I already have a circuit and need analysis.
- Why does the output voltage of an op amp need to be offset? Analysis of the principle of differential circuit
- [Mill MYC-J1028X development board trial] Use Node-RED to transform the MYC-J1028X development board into an industrial control gateway
- How to calculate power saving based on power factor?
- [Qinheng RISC-V core CH582] 4-The mobile phone reads and writes data from CH582