我正在尝试编辑特定内存地址中的字节,但是当偏移量传递给函数write_byte()
时,其偏移量将变为0。如果我对值进行硬编码,则可以使用,但是如果我使用scanf()
,则不能使用。
main()中的代码片段:
unsigned int offset, dumpsize;
char byte_data; // 8-bit operation.
puts("Please input your memory's offset address (in HEX):");
scanf("%x", &offset); // input an offset address (in HEX) to write.
puts("Please input your BYTE data to be written:");
scanf("%x", &byte_data); // input data.
write_byte (mem, offset, byte_data);
我的write_byte()
函数:
void write_byte(unsigned char *base_address, int offset, char byte_data){
memcpy(base_address + offset, &byte_data, sizeof(char));
}
这是它运行的一个示例:
The base address of your memory is: 7C9C4010h (HEX)
Please make a selection:
5
Please input your memory's offset address (in HEX, should be a multiply of 0x10h):
0
Please input the size of the memory to be dumped (between 256 and 1M ):
256
7C9C4010: 4C 93 F2 F4 1C B2 FA F3 07 AC 3A 3C 7A B8 39 89 --- L . . . . . . . . . : < z . 9 .
7C9C4020: 5A A8 EB 73 91 D5 9F 13 7D 38 05 61 03 70 B8 4F --- Z . . s . . . . } 8 . a . p . O
7C9C4030: 83 AC 45 9F 5F BF 93 E6 6C 4D A2 66 85 DB EF DF --- . . E . _ . . . l M . f . . . .
7C9C4040: 84 5B D3 16 B0 73 A9 AE AC AE 10 AF 1F C9 00 A2 --- . [ . . . s . . . . . . . . . .
7C9C4050: F5 C4 43 D4 84 56 BB 70 24 5F D6 29 3B 46 89 40 --- . . C . . V . p $ _ . ) ; F . @
7C9C4060: A2 5D 56 D2 D0 00 81 FC 2E 12 2D 4D 5B AC F0 51 --- . ] V . . . . . . . - M [ . . Q
7C9C4070: 71 B3 A5 75 89 62 E5 2D 41 BC 57 7C 03 E0 BC 25 --- q . . u . b . - A . W | . . . %
7C9C4080: 3E 93 78 8E 13 F9 8C 42 8B 39 0F 66 E5 00 37 57 --- > . x . . . . B . 9 . f . . 7 W
7C9C4090: 33 DD CC BD BF B2 6A 01 EE C1 FC F1 A2 3A 97 60 --- 3 . . . . . j . . . . . . : . `
7C9C40A0: CD 10 EF 60 89 FB 22 95 B4 32 FB 9A B1 34 71 65 --- . . . ` . . " . . 2 . . . 4 q e
7C9C40B0: 12 3E A2 D1 70 0D D2 DE CF 4F 50 F1 89 E7 53 57 --- . > . . p . . . . O P . . . S W
7C9C40C0: 77 C2 38 02 3E D9 97 F2 8B 93 8D BD 47 7E 23 D8 --- w . 8 . > . . . . . . . G ~ # .
7C9C40D0: BC 45 AA AC 52 FC 8B A1 4D DB 14 D6 44 E6 AE BB --- . E . . R . . . M . . . D . . .
7C9C40E0: 29 66 BD 67 40 D4 D9 4C E8 E6 0A 30 65 AC 0A A1 --- ) f . g @ . . L . . . 0 e . . .
7C9C40F0: F1 34 CD C3 B1 59 66 FE B4 F9 55 78 E0 83 35 0A --- . 4 . . . Y f . . . U x . . 5 .
7C9C4100: E9 72 F0 AA C7 CA F6 B0 31 01 60 96 2D E9 B7 1F --- . r . . . . . . 1 . ` . - . . .
The base address of your memory is: 7C9C4010h (HEX)
Please make a selection:
1
Please input your memory's offset address (in HEX):
12
Please input your BYTE data to be written:
AD
The base address of your memory is: 7C9C4010h (HEX)
Please make a selection:
5
Please input your memory's offset address (in HEX, should be a multiply of 0x10h):
0
Please input the size of the memory to be dumped (between 256 and 1M ):
256
7C9C4010: AD 93 F2 F4 1C B2 FA F3 07 AC 3A 3C 7A B8 39 89 --- . . . . . . . . . . : < z . 9 .
7C9C4020: 5A A8 EB 73 91 D5 9F 13 7D 38 05 61 03 70 B8 4F --- Z . . s . . . . } 8 . a . p . O
7C9C4030: 83 AC 45 9F 5F BF 93 E6 6C 4D A2 66 85 DB EF DF --- . . E . _ . . . l M . f . . . .
7C9C4040: 84 5B D3 16 B0 73 A9 AE AC AE 10 AF 1F C9 00 A2 --- . [ . . . s . . . . . . . . . .
7C9C4050: F5 C4 43 D4 84 56 BB 70 24 5F D6 29 3B 46 89 40 --- . . C . . V . p $ _ . ) ; F . @
7C9C4060: A2 5D 56 D2 D0 00 81 FC 2E 12 2D 4D 5B AC F0 51 --- . ] V . . . . . . . - M [ . . Q
7C9C4070: 71 B3 A5 75 89 62 E5 2D 41 BC 57 7C 03 E0 BC 25 --- q . . u . b . - A . W | . . . %
7C9C4080: 3E 93 78 8E 13 F9 8C 42 8B 39 0F 66 E5 00 37 57 --- > . x . . . . B . 9 . f . . 7 W
7C9C4090: 33 DD CC BD BF B2 6A 01 EE C1 FC F1 A2 3A 97 60 --- 3 . . . . . j . . . . . . : . `
7C9C40A0: CD 10 EF 60 89 FB 22 95 B4 32 FB 9A B1 34 71 65 --- . . . ` . . " . . 2 . . . 4 q e
7C9C40B0: 12 3E A2 D1 70 0D D2 DE CF 4F 50 F1 89 E7 53 57 --- . > . . p . . . . O P . . . S W
7C9C40C0: 77 C2 38 02 3E D9 97 F2 8B 93 8D BD 47 7E 23 D8 --- w . 8 . > . . . . . . . G ~ # .
7C9C40D0: BC 45 AA AC 52 FC 8B A1 4D DB 14 D6 44 E6 AE BB --- . E . . R . . . M . . . D . . .
7C9C40E0: 29 66 BD 67 40 D4 D9 4C E8 E6 0A 30 65 AC 0A A1 --- ) f . g @ . . L . . . 0 e . . .
7C9C40F0: F1 34 CD C3 B1 59 66 FE B4 F9 55 78 E0 83 35 0A --- . 4 . . . Y f . . . U x . . 5 .
7C9C4100: E9 72 F0 AA C7 CA F6 B0 31 01 60 96 2D E9 B7 1F --- . r . . . . . . 1 . ` . - . . .
正如您所看到的,0偏移是一个变化的,而不是0x12偏移。
编辑:我在上面运行了gdb,似乎在输入byte_data
值后,偏移值立即变为0。
The base address of your memory is: F7A7D010h (HEX)
Please make a selection:
1
Please input your memory's offset address (in HEX):
12
Please input your BYTE data to be written:
Breakpoint 3, setup_memory () at memory.c:175
175 scanf("%x", &byte_data); // input data.
(gdb) p offset
$3 = 18
(gdb) c
Continuing.
AD
Breakpoint 2, setup_memory () at memory.c:176
176 write_byte (mem, offset, byte_data); // write a byte to memory.
(gdb) p offset
$4 = 0
Edit2:这是一个解决方法,我决定尝试一下。我将偏移值分配给了全局变量unsigned int offset_g
。有效。但是,这可能是GCC错误吗?