flat assembler
Message board for the users of flat assembler.

Index > Linux > Getting memory error after ret

Author
Thread Post new topic Reply to topic
dstyl



Joined: 23 Jul 2015
Posts: 67
dstyl 06 Jan 2017, 16:20
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             r15, rdi
        call            loop
;-----------------------------------------------------------------------------|
loop:
        push    rdi                     ;sichere register die puts nutzt
        push            rsi                     ;//
        sub             rsp, 8          ;stack align auf 8byte vor call



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

        call            puts                    ;ausgabe des strings


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


        add             rsi, 8                  ;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             rsi, r15
        mov          rdi, format
        xor             rax, rax
        call           printf

        pop             rax
        pop             rcx
;DATA-------------------------------------------------------------------------|
format:
        db              "RDI = %d", 10, 0


    

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: 670
system error 06 Jan 2017, 17:27
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: 67
dstyl 06 Jan 2017, 17:32
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: 670
system error 06 Jan 2017, 17:35
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: 67
dstyl 06 Jan 2017, 18:04
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", 10, 0


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



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

        call    puts            ;ausgabe des strings

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


        add             rsi, 8          ;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             rsi, r15
        mov     rdi, format
        xor             rax, rax
        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: 61
_shura 28 Jan 2017, 10:43
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


Copyright © 1999-2025, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.