我正在尝试制作一个简单的操作系统内核的上半部分。当像我一样使用 Grub 作为引导加载程序时,还必须有一些下半部分(32 位)代码。因为我想让这个 32 位代码尽可能简短,所以我不想在其中编写一个 ELF 加载程序只是为了加载 64 位代码,因为这显然是荒谬的(这实际上是最常见的解决方案,但我如果可能的话想避免它)。
我发现链接描述文件允许加载与虚拟地址不同的地址。这很有用,这样我就可以加载 64 位部分以适应小型二进制文件,然后使用虚拟内存将正确的虚拟地址映射到它们加载的物理地址。除了低文本部分未放入文本段之外,此方法有效。入口点_start位于本节中。
除非我在命令中指定文本段,否则我无法将低文本部分(所在_start位置)放入文本段中PHDRS。当然,使用此命令使链接器决定放弃生成通常预期的段。当我也这样做时,这些部分最终会重叠,我不完全确定为什么。我按照数据、rodata、文本的顺序指定段,并且这些部分是相同的,但它们的加载内存地址是用rodata和交换的数据分配的,并且所有三个都是重叠的。
这是我的链接器脚本:
ENTRY(_start)
PHDRS {
.low PT_LOAD FILEHDR PHDRS;
.data PT_LOAD;
.rodata PT_LOAD;
.text PT_LOAD;
}
SECTIONS {
. = 1M;
.data_low BLOCK(4K) : ALIGN(4K) {
*(.bss_low)
} : .low
.rodata_low BLOCK(4K) : ALIGN(4K) {
KEEP(*(.multiboot_low))
*(.rodata_low)
} : .low
.text_low BLOCK(4K) : ALIGN(4K) {
*(.text_low)
} : .low
.stack 0xC0000000 : AT(0x200000) ALIGN(4K) {
*(.bootstrap_stack)
} : .data
_LADD_ = …Run Code Online (Sandbox Code Playgroud) 我正在尝试在Dafny中实现选择排序.
我sorted和FindMin函数确实有效,但selectionsort它本身包含Dafny无法证明的断言,即使它们是正确的.
这是我的计划:
predicate sorted(a:array<int>,i:int)
requires a != null;
requires 0 <= i <= a.Length;
reads a;
{
forall k :: 0 < k < i ==> a[k-1] < a[k]
}
method FindMin(a:array<int>,i:int) returns(m:int)
requires a != null;
requires 0 <= i < a.Length;
ensures i <= m < a.Length;
ensures forall k :: i <= k < a.Length ==> a[k] >= a[m];
{
var j := i;
m := i;
while(j < a.Length) …Run Code Online (Sandbox Code Playgroud) 我在Python中构建一个构造函数.当使用现有对象作为其输入进行调用时,应将"new"对象设置为该同一对象.这是一个10行演示:
class A:
def __init__(self, value):
if isinstance(value, A):
self = value
else:
self.attribute = value
a = A(1)
b = A(a)#a and b should be references to the same object
print("b is a", b is a)#this should be true: the identities should be the same
print("b == a", b == a)#this should be true: the values should be the same
Run Code Online (Sandbox Code Playgroud)
我希望对象A(a)从现有的对象构造的a是a.为什么不呢?要清楚,我想A(a)引用相同的对象a,而不是副本.
所以,我正在尝试用C创建这个二进制树程序,它只是不断抛出最糟糕的编译错误.每当我更改某些内容以找到导致错误的内容时,它就会发生变化!我没有忘记我的包含守卫,我曾经使用过#pragma.如果我将结构放在标题中,我会得到不兼容的类型,如果我把它放在.c中,我会重新定义pretty_print,当我重命名pretty_print时,它会消失.当我修剪bintree.c中的其他函数和标题时,错误更改为intTree未定义.如果我然后将intTree更改为int,则它变为不兼容的类型.如果我将标头合并到.c中,这就消失了.现在它已经消失了.我认为这些可能是单独的错误,而且变化的错误可能是我的坏事,但它正在发生,我不知道为什么.我将展示代码,演示尽可能多的这些,但每当我尝试复制我所做的时,它会抛出不同的错误.但我并不沮丧.我希望能够发布一个格式正确的问题和有用的信息,但我没有,尽管在这个程序上花了几个小时.
bintree.cxx
#include "bintree.h"
int add(intTree* root,int key,void* val){
if(!root){
root=malloc(sizeof(intTree));
if(!root){
return 1;
}
root->key=key;
root->val=val;
root->left=0;
root->right=0;
return 0;
}
if(key>root->key){
if(root->right){
return add(root->right,key,val);
}else{
root->right=(intTree*) malloc(sizeof(intTree));
if(!root->right){
return 1;
}
root->right->key=key;
root->right->val=val;
root->right->left=0;
root->right->right=0;
return 0;
}
}
if(key<root->key){
if(root->left){
return add(root->left,key,val);
}else{
root->left=malloc(sizeof(intTree));
if(!root->left){
return 1;
}
root->left->key=key;
root->left->val=val;
root->left->left=0;
root->left->right=0;
return 0;
}
}
return 2;
}
void* get(intTree* root,int key){
if(!root){
return 0;
}
if(key>root->key){
return get(root->right,key);
}
if(key<root->key){
return get(root->left,key);
}
return root->val; …Run Code Online (Sandbox Code Playgroud) c ×2
assembly ×1
assertions ×1
binary-tree ×1
constructor ×1
dafny ×1
elf ×1
header-files ×1
linker ×1
python ×1
self ×1
sorting ×1
struct ×1