如何在8086组件中的多个数据段之间切换? [英] How to switch between multiple data segments in 8086 assembly?

查看:125
本文介绍了如何在8086组件中的多个数据段之间切换?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在旧的Windows 95笔记本电脑上使用MASM 6.11进行编程,但是在数据段之间切换时遇到问题.

I'm programming using MASM 6.11 on an old Windows 95 laptop, and I'm having a problem switching between data segments.

为了便于组织,我想使用其他数据段来保存仅由宏使用的所有变量.该不同的数据段也放置在宏文件中.

For the sake of organization I wanted to use a different data segment to hold all of the variables that are only used by my macros. That different data segment is also placed inside the macro file.

我以为我可以通过仅将新段MOV指定到DS中来指定一个不同的数据段,但这似乎不起作用.组装时,我多次重复出现以下错误.

I thought I could specify a different data segment by just MOVing the new segment into DS, but that doesn't seem to be working. I get the following error repeated many times upon assembling.

错误A20068:无法使用段寄存器进行寻址

error A20068: Cannot address with segment register

这是一个示例.ASM.MAC文件,显示了我程序的基本布局.

Here's an example .ASM, and .MAC file showing my program's basic layout.

;********************
; STACK SEGMENT 
;********************

TheStack STACK SEGMENT

 DB 64 DUP ('THESTACK') ;512 bytes for the stack

TheStack ENDS

;********************
; END STACK SEGMENT
;********************

;********************
; DATA SEGMENT 
;********************

Data SEGMENT

var db ?

Data ENDS

;********************
; END DATA SEGMENT 
;********************


;********************
; CODE SEGMENT 
;********************

Code SEGMENT

assume CS:Code,DS:Data

MAIN PROC

    ;set Data to be the Data Segment
    mov ax, Data
    mov ds, ax

    MAC3   

    ;Return to DOS
    mov ah,4ch ;setup the terminate process DOS service
    mov al,0 ;ERRORLEVEL takes 0
    int 21h ;return to DOS

MAIN ENDP

Code ENDS

;********************
; END CODE SEGMENT 
;********************

END Start 

.MAC文件:

MAC1 MACRO

     mov MacVar1,bx

     ENDM


MAC2 MACRO

     mov MacVar2,cx

     ENDM



MAC3 MACRO

     mov ax, MacData
     mov ds, ax

     MAC1
     MAC2

     mov ax, Data
     mov ds, ax

     ENDM



;********************
; DATA SEGMENT 
;********************

MacData SEGMENT

macVar1 dw ?
macVar2 dw ?

MacData ENDS

;********************
; END DATA SEGMENT 
;********************

推荐答案

为了便于组织,我想使用一个不同的数据段来保存所有仅由宏使用的变量.

For the sake of organization I wanted to use a different data segment to hold all of the variables that are only used by my macros.

这听起来像是使您的宏变得不那么有用且更难使用的好方法.以及破坏性能.如果您想将宏与内存操作数一起使用怎么办?

That sounds like a good way to make your macros less useful and harder to use. As well as destroying performance. What if you want to use a macro with a memory operand?

如果您实际上并不在意8086,而只希望可以在386或更高版本上运行的16位代码,那么您仍然可以使用这个怪异的主意,但是对宏内的指令使用FS或GS段覆盖,而不是更改DS.

If you don't actually care about 8086, and just want 16-bit code that can run on a 386 or later, then you could still use this weird idea but with FS or GS segment overrides on instructions inside your macro, instead of changing DS.

甚至使用ES来实现8086兼容性,但请记住它是由字符串指令使用的.

Or even use ES for 8086 compatibility, but remember it's used by string instructions.

错误A20068:无法使用段寄存器进行寻址

error A20068: Cannot address with segment register

您没有说明是哪些指令在生成这些指令.

You didn't say which instructions are generating those.

; this is valid:
mov  ax, imm16
mov  ds, ax

如果这不是您的语法对MASM的含义,那么这就是您的问题.如果mov ax, MacData看起来像是对MASM的加载,那不好,但是IDK为什么会出现错误.也许它试图基于它认为不同变量应该存在的位置来隐式生成段替代?

If that's not what your syntax means to MASM, then that's your problem. If mov ax, MacData looks like a load to MASM, that's not good, but IDK why you get an error. Maybe it's trying to implicitly generate segment overrides based on where it thinks different variables are supposed to be?

请参见标签Wiki的问题,以获取指向以下链接的链接汇编器手册和许多其他内容.

See the x86 tag wiki for links to assembler manuals and lots of other stuff.

通常,IMO中,分段16位代码不是学习asm的好方法.特别是不要同时学习DOS系统调用API,因为这是过时的知识,您在查看真实程序的组装时(调试或性能调整时)不会使用.请参阅此答案,以详细了解我认为16位代码是一种不好的学习方式.

Segmented 16-bit code is not a good way to learn asm in general, IMO. Especially not learning the DOS system call API at the same time, since that's obsolete knowledge that you won't use while looking at the asm of real programs (while debugging or performance tuning). See this answer for more about how I think 16-bit code is a bad way to learn.

这篇关于如何在8086组件中的多个数据段之间切换?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆