stdout - x86 assembly: printing integer to the console after mul (seg fault) -


i'm trying learn x86 assembly. book i'm using assembly language - step step, programming linux (and i'd have it's pretty good). i've learned lot far, feel though should challenging myself stay ahead in many respects can learn faster through doing (i can follow along, top-down learning, find tediously slow).

so, figured cool idea try , multiply 2 registers (32-bit) , output data console.

the problem when execute program (i'm using nasm, book - no insight debugger though), receive segmentation fault. i've done fair amount of debugging in gdb little hammer out, whatever reason can't seem figure out issue is.

i'd know why i'm receiving segmentation fault, , way reprimand issue. also, if comments i've made in code don't match happening, i'd grateful if correct me on that.

here's code far (it's commented)

thanks.

teh codez

section .data ;todo  section .bss valuetoprint: resb 4            ;alloc 4 bytes of data in 'valuetoprint'  section .text  global _start  _mul:     mov eax, 0x2a ;store 42 in eax     mov edx, 0x2a ;store 42 in edx     mul eax     ret  _safe_exit:     mov eax, 1  ;initiate 'exit' syscall     mov ebx, 0  ;exit error code 0     int 0x80    ;invoke kernel bidding   _start:     nop                         ;used keep gdb complaining      call _mul                       ;multiply values     mov [valuetoprint], eax         ;store address of eax in contents of valuetoprint     mov eax, 4                      ;specify system write call - aka syswrite     mov ebx, 1                      ;direction used make syswrite call output console - i.e. stdout     mov dword [ecx], valuetoprint   ;store valuetoprint in ecx: ecx represents syswrite register     int 0x80                        ;invoke kernel based on given parameters      call _safe_exit 

edit

also, i'm running arch linux, if makes difference.

this line causing segmentation fault:

mov dword [ecx], valuetoprint 

you're telling store valuetoprint in memory location @ address ecx. never initialize ecx (the kernel initializes 0 on program start you), when dereference it, you're going access invalid memory location.

the write(2) system call takes 3 parameters: file descriptor number in register ebx, pointer string write in ecx, , number of bytes write in edx. so, if want print raw binary data of result, can pass address of valuetoprint, , tell print 4 bytes address. in case, valuetoprint 1764 (0x6e4 in hex), code print out 4 bytes e4 06 00 00 on x86, little-endian:

mov [valuetoprint], eax   ; store result memory mov eax, 4                ; system call #4 = sys_write mov ebx, 1                ; file descriptor 1 = stdout mov ecx, valuetoprint     ; store *address* of valuetoprint ecx mov edx, 4                ; write out 4 bytes of data int 0x80                  ; syscall 

Comments

Popular posts from this blog

c# - SVN Error : "svnadmin: E205000: Too many arguments" -

c# - Copy ObservableCollection to another ObservableCollection -

All overlapping substrings matching a java regex -