flat assembler
Message board for the users of flat assembler.
 Home   FAQ   Search   Register 
 Profile   Log in to check your private messages   Log in 
flat assembler > Linux > Getting memory error after ret

Author
Thread Post new topic Reply to topic
dstyl



Joined: 23 Jul 2015
Posts: 48
Getting memory error after ret

Code:

; -----------------------------------------------------------------------------
; A 64-bit program that displays its command line arguments, one per line.
;
; On entry, rdi will contain argc and rsi will contain argv.
; -----------------------------------------------------------------------------

        global  main

        extern  puts

        extern  printf

        section .text
;CODE-------------------------------------------------------------------------|
main:
        mov             r15rdi
        call            loop
;-----------------------------------------------------------------------------|
loop:
        push    rdi                     ;sichere register die puts nutzt
        push            rsi                     ;//
        sub             rsp8          ;stack align auf 8byte vor call



        mov             rdi, [rsi]              ;verschiebt die speicheradresse 
                                                ;des argument strings in rdi

        call            puts                    ;ausgabe des strings


        add             rsp8          ;wiederherstellung des stack-
                                                ;pointers auf den alten wert.
        pop             rsi                     ;wiederherstellen der register
        pop             rdi                     ;rheinfolge beachten!


        add             rsi8                  ;Zeige auf nächstes arg
        dec             rdi                     ;Zähle argc runter
        
        jnz             loop                    ;falls argc != 0 mache weiter

        call            print
        ret
;-----------------------------------------------------------------------------|
print:
        push            rax
        push    rcx

        mov             rsir15
        mov          rdiformat
        xor             raxrax
        call           printf

        pop             rax
        pop             rcx
;DATA-------------------------------------------------------------------------|
format:
        db              "RDI = %d"100





The programm shows the argc and argv but it fails after exit on an memory error.
Could someone please help me?
Thx in advance Wink
Syntax is NASM.
Post 06 Jan 2017, 16:20
View user's profile Send private message Reply with quote
system error



Joined: 01 Sep 2013
Posts: 673
You are misplacing, missing and abusing RET instruction. Your logical path crosses many scope boundaries of a procedure.
Post 06 Jan 2017, 17:27
View user's profile Send private message Reply with quote
dstyl



Joined: 23 Jul 2015
Posts: 48
Ok thanks for your help Smile Can you help me to correct the code please because im a noob in asm.
Post 06 Jan 2017, 17:32
View user's profile Send private message Reply with quote
system error



Joined: 01 Sep 2013
Posts: 673
It should be something like


Code:
procedureA:

    call procedureB
ret

;----> your main function crosses this boundary due to missing ret statement

procedureB:

   call procedureC
ret

proceduceC:

;----> again, you're missing the RET here and your code executes your data 



And put your data in a proper data section. I don't know how NASM allows a LOOP instruction to be used as a label name either. Maybe u should verify that first. Doesn't look right to me.
Post 06 Jan 2017, 17:35
View user's profile Send private message Reply with quote
dstyl



Joined: 23 Jul 2015
Posts: 48

Code:

; -----------------------------------------------------------------------------
; A 64-bit program that displays its command line arguments, one per line.
;
; On entry, rdi will contain argc and rsi will contain argv.
; -----------------------------------------------------------------------------

        global  main

        extern  puts

        extern  printf

        section .data
;DATA-------------------------------------------------------------------------|
format:
        db      "RDI = %d"100


        section .text
;CODE-------------------------------------------------------------------------|
main:
        mov             r15rdi
        call    count
        ret
;-----------------------------------------------------------------------------|
count:
        push    rdi                     ;sichere register die puts nutzt
        push    rsi                     ;//
        sub             rsp8          ;stack align auf 8byte vor call



        mov             rdi, [rsi]      ;verschiebt die speicheradresse 
                                                ;des argument strings in rdi

        call    puts            ;ausgabe des strings

        add             rsp8          ;wiederherstellung des stack-
                                                ;pointers auf den alten wert.
        pop             rsi                     ;wiederherstellen der register
        pop             rdi                     ;rheinfolge beachten!


        add             rsi8          ;Zeige auf nächstes arg
        dec             rdi                     ;Zähle argc runter
        
        jnz             count           ;falls argc != 0 mache weiter

        call    print
        ret

;-----------------------------------------------------------------------------|
print:
        push    rax
        push    rcx

        mov             rsir15
        mov     rdiformat
        xor             raxrax
        call    printf

        pop             rax
        pop             rcx
        




When i put the data in front of the code as own section it works fine seems to be the trouble with the missing data section. i dont know how to add an special data section in NASM but your advice fixed it thanks for your help Wink
Post 06 Jan 2017, 18:04
View user's profile Send private message Reply with quote
_shura



Joined: 22 May 2015
Posts: 60
1. print needs an ret too
2. some optimization:

Code:

  jmp print


Post 28 Jan 2017, 10:43
View user's profile Send private message Visit poster's website Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >

Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001-2005 phpBB Group.

Main index   Download   Documentation   Examples   Message board
Copyright © 2004-2016, Tomasz Grysztar.