64bit - Unable to find length of String in Assembly(nasm Linux) -
i have made program takes string input user , calculates length , display it. getting error segmentation fault (core dumped) when run program. code shown below
str_len: ;procedure calculate length of string xor rdx,rdx xor rcx,rcx ;i want store length in rcx mov rdx,[string] ;string contains input taken user mov rsi,rdx mov rcx,'0' ;by default rcx contain '0' decimal up: cmp byte[rsi],0 ;compare if end of string je str_o ;if yes jump str_o inc rcx ;increment rcx i.e., length inc rsi ;point next location of rdx i.e.,string jmp ;repeat till end of string str_o: mov rax,1 ;print final length mov rdi,1 mov rsi,rcx mov rdx,1 syscall ret i can guarantee rest of program correct. error in above part of code. error ?
the error here:
mov rdx,[string] ; string contains input taken user you loading rdx first 8 bytes of string content , not address of string. better use
lea rdx, string ; lea = load effective address another problem output routine tries print number not converted ascii with
mov rax,1 ; sys_write - print final length mov rdi,1 ; stdout handle mov rsi,rcx ; rsi should point buffer mov rdx,1 ; length of buffer - value of 1 prints 1 char syscall this doesn't work way. you'd have convert qword number in rcx ascii string before , pass address of string in rsi. search stack overflow questions this.
Comments
Post a Comment