hi all what wrong with this code? Код (Text): BITS 32 section .data format db "[0x%x] -> %d",10,13,0 section .text global main extern printf,exit main: mov esi,140h while_loop: cmp esi,150h jg while_end ;loop body mov edx,[esi]; why segmentation fault? push edx push esi push format call printf sub esp,12 inc esi jmp while_loop while_end: call exit ------------------------------------------- It produces segmentation fault.I compiled it under Debian (arch k7).The line with comment is problematic-if replace it with mov edx,esi it runs.Output: Код (Text): [0x140] -> 320 [0x141] -> 321 [0x142] -> 322 [0x143] -> 323 [0x144] -> 324 [0x145] -> 325 [0x146] -> 326 [0x147] -> 327 [0x148] -> 328 [0x149] -> 329 [0x14a] -> 330 [0x14b] -> 331 [0x14c] -> 332 [0x14d] -> 333 [0x14e] -> 334 [0x14f] -> 335 [0x150] -> 336 I can't see any problem with that line...
You're trying to read a memory block starting at address 0x140. That block is outside your program sections (.data, .text, ...) You can't just go there and read a piece of memory wich is not your's. Try this: Код (Text): BITS 32 section .data format db "[0x%x] -> %d",10,13,0 section .text global main extern printf,exit main: mov esi,main while_loop: cmp esi,main + 10h jg while_end ;loop body mov edx,[esi]; why segmentation fault? push edx push esi push format call printf sub esp,12 inc esi jmp while_loop while_end: call exit This way you can read some bytes starting at your program's entry point.
hi Quantum.Thanks for answering... I make another try to read a memory block.Looks like block again outside my program sections,but this time there is now segmentation fault.Here is same code with little change: Код (Text): BITS 32 section .data format db "[0x%x] -> %d",10,13,0 mem dd -3000 section .text global main extern printf,exit main: mov esi,1h while_loop: cmp esi,10h jg while_end ;loop body mov edx,[mem+esi]; NO SEGMENTATION FAULT ! push edx push esi push format call printf sub esp,12 inc esi jmp while_loop while_end: call exit So,a am starting scanning from address [-3000+esi]... this is the output: Код (Text): [0x1] -> 16777204 [0x2] -> 65535 [0x3] -> 255 [0x4] -> 0 [0x5] -> 0 [0x6] -> 16777216 [0x7] -> 65536 [0x8] -> 256 [0x9] -> 1 [0xa] -> 603979776 [0xb] -> 2359296 [0xc] -> 9216 [0xd] -> 36 [0xe] -> 201326592 [0xf] -> 786432 [0x10] -> 3072 [...] is value of esi,the value in second column is taken from memory at [-3000+esi].( which is not my memory? ) May be this is some kind of restriction applyed by NASM compiler?
No, you starting to scan at [offset mem + esi], because mem is a lable. That's almost the -3000 value you assigned to mem. mem <= -3000 == 0xFFFFF448. Words and double words get stored in memory in little endian format. So, 0xFFFFF448 looks like 48 F4 FF FF. But you're starting to scan at mem + 1 and getting 16777204 == 0xFFFFF4. In little endian it's F4 FF FF 00. Got it?