Author Topic: Why Does it print two blank line when exit  (Read 6019 times)

Offline Beta

  • Jr. Member
  • *
  • Posts: 3
Why Does it print two blank line when exit
« on: June 27, 2020, 11:52:23 AM »
Hi , this is my first post

I dont understand why this code prints 2 blank line when it exit

(Linux bash)

Code: [Select]
     section .data

        msg             db      "Hello I'm Nasm !!!!!!!",10     ; 10  (ritorno a capo)
        msgend          equ $
        msgTot          db      "first value: "
        msgEndTot       equ $



        section .text
        global _start


_start:
        ;Welcome message
        mov rax,1                       ; 1 = sys_write syscall number
        mov rdi,1                       ; 1 = STDOUT file descriptor
        mov rsi,msg                     ; message's buffer
        mov rdx,msgend-msg              ; Calculate the len(message)
        syscall                         ; Kernel calls


        ;Input first message
        mov rax,1                       ; 1 = sys_write syscall number
        mov rdi,1                       ; 1 = STDOUT file descriptor
        mov rsi,msgTot                  ; message's buffer
        mov rdx,msgend-msg              ; Calculate the len(message)
        syscall                         ; Kernel calls



        ;Request first value
        mov rax,0                       ; 0 = sys_read syscall number
        sub rsp,8                       ; Allocates 8 byte nello stack
        sub rbx,0x30                    ; Convert Ascii to dec
        and rbx,0xff                    ; ensures high bytes are set to zero

        mov rdi,0                       ; 0 = STDIN file descriptor
        lea rsi,[rsp]                   ; char *buff points to stack address allocated space
        mov rdx,1                       ; size_t = 1 
        syscall                         ; Kernel calls

        mov rdi,0                       ; returns zero
        mov rax,60                      ; Exit calls

        syscall                         ; Kernel calls

output:
Quote
beta@beta:~/TestC$ ./test
Hello I'm Nasm !!!!!!!
first value: 1
beta@beta:~/TestC$
beta@beta:~/TestC$

« Last Edit: June 27, 2020, 11:59:22 AM by Beta »

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: Why Does it print two blank line when exit
« Reply #1 on: June 27, 2020, 08:17:40 PM »
Hi Beta,

Welcome to the forum.

sys_read does not return until the "enter" key is hit. With just 1 in rdx, you do not get this. Get at least 2. Throw away the linefeed if you don't want it. I'm not in the mood to fool with this, but I think that'll solve your problem...

Best,
Frank


Offline Beta

  • Jr. Member
  • *
  • Posts: 3
Re: Why Does it print two blank line when exit
« Reply #2 on: July 02, 2020, 05:08:11 PM »
That has solved the issue, but  I have more questions about asm :D

Offline Ferran

  • Jr. Member
  • *
  • Posts: 23
  • Country: ad
Re: Why Does it print two blank line when exit
« Reply #3 on: September 06, 2020, 11:37:38 AM »
Hello Beta:

I'm happy you finally solved this issue, but please, can you bring us the code?. It could be interesting by the rest of members.

Anyway, i did this, but i don't know to convert strings to number yet (i'm a newbie):

Code: [Select]
section .data
        msg             db      "Hello I'm Nasm !!!!!!!",10     ; 10  (ritorno a capo)
        msgend          equ $ - msg                             ; <--- length of msg
        msgTot          db      "first value: "                       
        msgEndTot       equ $ - msgTot                          ; <--- length of msgTot

section .bss
        rqst_space      equ 8                                   ; <--- reserve 8 bytes to fill
        value          resb rqst_space                          ; <--- lenght of value

section .text
global _start

_start:
        ;Welcome message
        mov rax,1                        ; 1 = sys_write syscall number
        mov rdi,1                        ; 1 = STDOUT file descriptor
        mov rsi,msg                      ; message's buffer
        mov rdx,msgend                   ; <--- input the length
        syscall                          ; Kernel calls

        ;Input first message
        mov rax,1                        ; 1 = sys_write syscall number
        mov rdi,1                        ; 1 = STDOUT file descriptor
        mov rsi,msgTot                   ; message's buffer
        mov rdx,msgEndTot                ; <--- input the length
        syscall                          ; Kernel calls

        ;Request first value
        mov rax, 0                       ; 0=sys_read syscall number
        mov rdi, 0                       ; 0=STDIN file descriptor
        mov rsi, value                   ; <--- loading the value
        mov rdx, rqst_space              ; <--- length of the input field
        syscall

        ;Print the first value
        mov rax, 1                       ; 1=sys_write syscall number
        mov rdi, 1                       ; 1=STDOUT file descriptor
        mov rsi, value                   ; <--- unloading the value in display
        mov rdx, rqst_space              ; <--- lenght of field
        syscall

        ; exit
        xor edi, edi                     ; <--- returns zero
        mov rax,60                       ; Exit calls
        syscall                          ; Kernel calls


Ci vediamo !  ;D