今天看到个奇怪的代码,求科普

yajuie 2015-09-28 11:53:04
今天看到一个奇怪的代码。 unsigned long a;//这里是我找到的变量定义。 /*下面是我,感觉奇怪的地方*/ a >>= 32; a <<= 13; /*奇怪的第一个点, a>>=32; 我拆分出来,a= a>>32;这种结果我计算机出是0; 奇怪第二点,a <<=13;如果前面是0,那紧跟这一句,不是多此一举。 long 做为关键字,难道还可以使用 自定义,宏替换之类的手法,把long long 改成long. 何况书还是9几年写的,难道那个时候64位pc机就以经,普及了, */
...全文
292 9 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
mymtom 2015-09-30
  • 打赏
  • 举报
回复
楼上真强大啊! ALPHA处理器(1993),20多年了,最早的64位架构之一, Linux用的LP64,所以long是64位。
logiciel 2015-09-30
  • 打赏
  • 举报
回复
Linux/arch/alpha/boot/main.c 1 /* 2 * arch/alpha/boot/main.c 3 * 4 * Copyright (C) 1994, 1995 Linus Torvalds 5 * 6 * This file is the bootloader for the Linux/AXP kernel 7 */ 8 #include <linux/kernel.h> 9 #include <linux/slab.h> 10 #include <linux/string.h> 11 #include <generated/utsrelease.h> 12 #include <linux/mm.h> 13 14 #include <asm/console.h> 15 #include <asm/hwrpb.h> 16 #include <asm/pgtable.h> 17 18 #include <stdarg.h> 19 20 #include "ksize.h" 21 22 extern unsigned long switch_to_osf_pal(unsigned long nr, 23 struct pcb_struct * pcb_va, struct pcb_struct * pcb_pa, 24 unsigned long *vptb); 25 struct hwrpb_struct *hwrpb = INIT_HWRPB; 26 static struct pcb_struct pcb_va[1]; 27 28 /* 29 * Find a physical address of a virtual object.. 30 * 31 * This is easy using the virtual page table address. 32 */ 33 34 static inline void * 35 find_pa(unsigned long *vptb, void *ptr) 36 { 37 unsigned long address = (unsigned long) ptr; 38 unsigned long result; 39 40 result = vptb[address >> 13]; 41 result >>= 32; 42 result <<= 13; 43 result |= address & 0x1fff; 44 return (void *) result; 45 } 46 47 /* 48 * This function moves into OSF/1 pal-code, and has a temporary 49 * PCB for that. The kernel proper should replace this PCB with 50 * the real one as soon as possible. 51 * 52 * The page table muckery in here depends on the fact that the boot 53 * code has the L1 page table identity-map itself in the second PTE 54 * in the L1 page table. Thus the L1-page is virtually addressable 55 * itself (through three levels) at virtual address 0x200802000. 56 */ 57 58 #define VPTB ((unsigned long *) 0x200000000) 59 #define L1 ((unsigned long *) 0x200802000) 60 61 void 62 pal_init(void) 63 { 64 unsigned long i, rev; 65 struct percpu_struct * percpu; 66 struct pcb_struct * pcb_pa; 67 68 /* Create the dummy PCB. */ 69 pcb_va->ksp = 0; 70 pcb_va->usp = 0; 71 pcb_va->ptbr = L1[1] >> 32; 72 pcb_va->asn = 0; 73 pcb_va->pcc = 0; 74 pcb_va->unique = 0; 75 pcb_va->flags = 1; 76 pcb_va->res1 = 0; 77 pcb_va->res2 = 0; 78 pcb_pa = find_pa(VPTB, pcb_va); 79 80 /* 81 * a0 = 2 (OSF) 82 * a1 = return address, but we give the asm the vaddr of the PCB 83 * a2 = physical addr of PCB 84 * a3 = new virtual page table pointer 85 * a4 = KSP (but the asm sets it) 86 */ 87 srm_printk("Switching to OSF PAL-code .. "); 88 89 i = switch_to_osf_pal(2, pcb_va, pcb_pa, VPTB); 90 if (i) { 91 srm_printk("failed, code %ld\n", i); 92 __halt(); 93 } 94 95 percpu = (struct percpu_struct *) 96 (INIT_HWRPB->processor_offset + (unsigned long) INIT_HWRPB); 97 rev = percpu->pal_revision = percpu->palcode_avail[2]; 98 99 srm_printk("Ok (rev %lx)\n", rev); 100 101 tbia(); /* do it directly in case we are SMP */ 102 } 103 104 static inline long openboot(void) 105 { 106 char bootdev[256]; 107 long result; 108 109 result = callback_getenv(ENV_BOOTED_DEV, bootdev, 255); 110 if (result < 0) 111 return result; 112 return callback_open(bootdev, result & 255); 113 } 114 115 static inline long close(long dev) 116 { 117 return callback_close(dev); 118 } 119 120 static inline long load(long dev, unsigned long addr, unsigned long count) 121 { 122 char bootfile[256]; 123 extern char _end; 124 long result, boot_size = &_end - (char *) BOOT_ADDR; 125 126 result = callback_getenv(ENV_BOOTED_FILE, bootfile, 255); 127 if (result < 0) 128 return result; 129 result &= 255; 130 bootfile[result] = '\0'; 131 if (result) 132 srm_printk("Boot file specification (%s) not implemented\n", 133 bootfile); 134 return callback_read(dev, count, (void *)addr, boot_size/512 + 1); 135 } 136 137 /* 138 * Start the kernel. 139 */ 140 static void runkernel(void) 141 { 142 __asm__ __volatile__( 143 "bis %1,%1,$30\n\t" 144 "bis %0,%0,$26\n\t" 145 "ret ($26)" 146 : /* no outputs: it doesn't even return */ 147 : "r" (START_ADDR), 148 "r" (PAGE_SIZE + INIT_STACK)); 149 } 150 151 void start_kernel(void) 152 { 153 long i; 154 long dev; 155 int nbytes; 156 char envval[256]; 157 158 srm_printk("Linux/AXP bootloader for Linux " UTS_RELEASE "\n"); 159 if (INIT_HWRPB->pagesize != 8192) { 160 srm_printk("Expected 8kB pages, got %ldkB\n", INIT_HWRPB->pagesize >> 10); 161 return; 162 } 163 pal_init(); 164 dev = openboot(); 165 if (dev < 0) { 166 srm_printk("Unable to open boot device: %016lx\n", dev); 167 return; 168 } 169 dev &= 0xffffffff; 170 srm_printk("Loading vmlinux ..."); 171 i = load(dev, START_ADDR, KERNEL_SIZE); 172 close(dev); 173 if (i != KERNEL_SIZE) { 174 srm_printk("Failed (%lx)\n", i); 175 return; 176 } 177 178 nbytes = callback_getenv(ENV_BOOTED_OSFLAGS, envval, sizeof(envval)); 179 if (nbytes < 0) { 180 nbytes = 0; 181 } 182 envval[nbytes] = '\0'; 183 strcpy((char*)ZERO_PGE, envval); 184 185 srm_printk(" Ok\nNow booting the kernel\n"); 186 runkernel(); 187 for (i = 0 ; i < 0x100000000 ; i++) 188 /* nothing */; 189 __halt(); 190 } 191
bluewanderer 2015-09-29
  • 打赏
  • 举报
回复
首先那会儿比如N64虽然比较失败也卖出去不少来的。然后long到底多少位完全看写编译器的人的心情,现在Win64上long也还是32位。最后不知道你这本书的上下文。
mewiteor 2015-09-29
  • 打赏
  • 举报
回复
fly_dragon_fly 2015-09-29
  • 打赏
  • 举报
回复
书中程序用在什么地方
mujiok2003 2015-09-29
  • 打赏
  • 举报
回复
不要假设long 是32位的
yajuie 2015-09-29
  • 打赏
  • 举报
回复
引用 3 楼 fly_dragon_fly的回复:
书中程序用在什么地方
书是,从宿舍找到的,残破不堪。没几段完整的代码,刚好不多的完整代码中有这么段完整函数,函数完整如下: void* find_pa(unsigned long *vptb,void *ptr) { unsigned long address = (unsigned long) ptr; unsigned long result; result = vptb[address >> 32]; result >>= 32; result <<=13; result |= address & 0x1fff; return (void *)result; }
yajuie 2015-09-28
  • 打赏
  • 举报
回复
求各路大神,指点一下,这到底在什么情况下会写成这样,因为书上这种代码以出现了好几遍了,基本上可以排出bug的可能,

70,026

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧