Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/opengl/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C+的组织备选方案+;_C++_Memory_Assembly_Operating System_Ram - Fatal编程技术网

C++ C+的组织备选方案+;

C++ C+的组织备选方案+;,c++,memory,assembly,operating-system,ram,C++,Memory,Assembly,Operating System,Ram,在汇编中,我们使用org指令将位置计数器设置为内存中的特定位置。这对于制作操作系统特别有帮助。下面是一个引导加载程序示例(来自wikibooks): 可以用C++完成任务吗?是否有任何命令、函数等,比如org,我可以在其中更改程序的位置?不,据我所知,在任何C编译器中都不可能这样做。但是,您可以创建自己的,将代码/数据/bss段放置在特定地址。为了清楚起见,org指令不会在指定地址加载代码,它只通知汇编程序将在该地址加载代码。显示的代码似乎是针对Nasm(或类似)的-在AT&T语法中,.org指

在汇编中,我们使用
org
指令将位置计数器设置为内存中的特定位置。这对于制作操作系统特别有帮助。下面是一个引导加载程序示例(来自wikibooks):


可以用C++完成任务吗?是否有任何命令、函数等,比如
org
,我可以在其中更改程序的位置?

不,据我所知,在任何C编译器中都不可能这样做。但是,您可以创建自己的,将代码/数据/bss段放置在特定地址。

为了清楚起见,
org
指令不会在指定地址加载代码,它只通知汇编程序将在该地址加载代码。显示的代码似乎是针对Nasm(或类似)的-在AT&T语法中,
.org
指令做了一些不同的事情:它将代码填充到该地址-类似于Nasm代码中的
times
行。。Nasm可以做到这一点,因为在
-f bin
模式下,它“充当自己的链接器”

代码需要知道的重要一点是可以找到
Msg
的地址。
jmp
s和
jne
s(和
call
ret
,您的示例没有,但编译器可能会生成)是相对寻址模式。我们编码
jmp-target
,但实际发出的字节表示
jmp-distance\u-to\u-target
,因此地址无关紧要

Gas不会这样做,它会发出一个可链接的对象文件。要在不使用链接器脚本的情况下使用ld,命令行如下所示:

ld-o boot.bin boot.o-oformat binary-T text=0x7C00

(不要引用我的确切语法,但“类似的东西”)如果你能从你的(16位有能力)C++编译器中获得一个可链接的对象文件,你也可以这样做。 在引导扇区的情况下,代码由BIOS(或假BIOS)在0x7C00处加载-这是我们可以假设的关于引导扇区的少数事情之一。bootsector要做的明智的事情不是在打印消息时胡乱摆弄,而是加载其他内容。你需要知道如何找到磁盘上的其他东西,以及你想把它加载到哪里(也许是C++编译器默认要放置的地方)->代码> JMP < /Cord>。这个

jmp
将希望成为一个
far jmp
,它确实需要知道地址


我猜它会是一些难看的C++! 所以你的意思是,我必须使用汇编来启动加载程序,而不可能使用任何其他语言?@AnshumanDwibhashi不,你仍然可以使用链接器脚本来设置地址。然而,对于第一步引导加载程序来说,用汇编程序编写它,然后调用相应的高级代码通常比较容易。好的,谢谢@JoachimPileborg,但你能给出一些示例代码吗?@AnshumanDwibhashi不是用C编写的第一级引导加载程序。我从来没有用C编写过,也从来没有见过一个,但这绝对是可能的。不@JoachinPileborg我指的不是引导加载程序的例子,我指的是用你规定的方法转移内存的例子
 org 7C00h

         jmp short Start ;Jump over the data (the 'short' keyword makes the jmp instruction smaller)

 Msg:    db "Hello World! "
 EndMsg:

 Start:  mov bx, 000Fh   ;Page 0, colour attribute 15 (white) for the int 10 calls below
         mov cx, 1       ;We will want to write 1 character
         xor dx, dx      ;Start at top left corner
         mov ds, dx      ;Ensure ds = 0 (to let us load the message)
         cld             ;Ensure direction flag is cleared (for LODSB)

 Print:  mov si, Msg     ;Loads the address of the first byte of the message, 7C02h in this case

                         ;PC BIOS Interrupt 10 Subfunction 2 - Set cursor position
                         ;AH = 2
 Char:   mov ah, 2       ;BH = page, DH = row, DL = column
         int 10h
         lodsb           ;Load a byte of the message into AL.
                         ;Remember that DS is 0 and SI holds the
                         ;offset of one of the bytes of the message.

                         ;PC BIOS Interrupt 10 Subfunction 9 - Write character and colour
                         ;AH = 9
         mov ah, 9       ;BH = page, AL = character, BL = attribute, CX = character count
         int 10h

         inc dl          ;Advance cursor

         cmp dl, 80      ;Wrap around edge of screen if necessary
         jne Skip
         xor dl, dl
         inc dh

         cmp dh, 25      ;Wrap around bottom of screen if necessary
         jne Skip
         xor dh, dh

 Skip:   cmp si, EndMsg  ;If we're not at end of message,
         jne Char        ;continue loading characters
         jmp Print       ;otherwise restart from the beginning of the message


 times 0200h - 2 - ($ - $$)  db 0    ;Zerofill up to 510 bytes

         dw 0AA55h       ;Boot Sector signature

 ;OPTIONAL:
 ;To zerofill up to the size of a standard 1.44MB, 3.5" floppy disk
 ;times 1474560 - ($ - $$) db 0