Navigacija
Lista poslednjih: 16, 32, 64, 128 poruka.

Read and print int u assembly (MIPS) programu

[es] :: Asembler :: Read and print int u assembly (MIPS) programu

[ Pregleda: 1929 | Odgovora: 4 ] > FB > Twit

Postavi temu Odgovori

Autor

Pretraga teme: Traži
Markiranje Štampanje RSS

vidonk
Slobodan Vidovic
Niksic

Član broj: 192156
Poruke: 354
*.crnagora.net.



+9 Profil

icon Read and print int u assembly (MIPS) programu28.03.2017. u 18:42 - pre 85 meseci
Pokušavam da napišem program koji će da sačuva unesen cio broj i kasnije da ga "odštampa" u konzoli.
Prvi poblem sa kojim se susrećem jeste koji sistemski call da pozovem a koji je definisan u /usr/mips-linux-gnu/include/asm/unistd.h.

Code:
#ifndef _ASM_UNISTD_H
#define _ASM_UNISTD_H

#include <asm/sgidefs.h>

#if _MIPS_SIM == _MIPS_SIM_ABI32

/*
 * Linux o32 style syscalls are in the range from 4000 to 4999.
 */
#define __NR_Linux            4000
#define __NR_syscall            (__NR_Linux +    0)
#define __NR_exit            (__NR_Linux +    1)
#define __NR_fork            (__NR_Linux +    2)
#define __NR_read            (__NR_Linux +    3)
#define __NR_write            (__NR_Linux +    4)
#define __NR_open            (__NR_Linux +    5)
#define __NR_close            (__NR_Linux +    6)


Mislim da je to __NR_read I stvarno nakon

Code:
#Raad the integer and save it in s0
    li    v0,__NR_read
    syscall
    move s0, v0


program čeka da se unese input, dobro ja unesem recimo proj 9 ali u registru v0 i s0 nalazi se 0x2 ?

EDIT:

Ovako našao sam ovo
Code:
asmlinkage long sys_read(unsigned int fd, char __user *buf, size_t count);

to su dake argumenti koje zahtjeva __NR_read, dobro ovu bi bila moja pitanja
1. prvi argument je fd znači za stdin je 0 ?
2. drugi argument je char __user *buf to znači da je pointer prema bufferu, pointer prema nekoj memorijskoj lokaciji ? Probao sam da stavim ovako

Code:
#Read the integer and save it in s0
    nop
    nop
    li    a0,0        # firs argument, for stdin is 0 ISTR, see "man 2 read"
    la    a1,(var1)    # second argument load adress of var1 in to a1
    li    a2,10     # third argument is count of byts 
    li    v0,__NR_read
    syscall
    .data
    var1:       .space    12

ali dobijam SIGSEGV, Segmentation fault.
3. treći argument je size_t count znači neki broj ? ali čega ?



[Ovu poruku je menjao vidonk dana 29.03.2017. u 15:16 GMT+1]
Signaure Hamer Dev .inc
 
Odgovor na temu

Branimir Maksimovic

Član broj: 64947
Poruke: 5534
c-bg-d-p1-76.bvcom.net.



+1064 Profil

icon Re: Read and print int u assembly (MIPS) programu29.03.2017. u 03:01 - pre 85 meseci
Mislim da treba da napravis da podaci budu u writeable .data sekciji. Kako to da uradis u ovom asembleru, ne znam, ali trazi u tom pravcu.

edit: wuick google mi je dao ovo:
.data # Tells assembler we're in the data segment
val: .word 10, -14, 30 # Three words placed in memory
 
Odgovor na temu

vidonk
Slobodan Vidovic
Niksic

Član broj: 192156
Poruke: 354
*.crnagora.net.



+9 Profil

icon Re: Read and print int u assembly (MIPS) programu29.03.2017. u 14:24 - pre 85 meseci
Citat:
Branimir Maksimovic: Mislim da treba da napravis da podaci budu u writeable .data sekciji. Kako to da uradis u ovom asembleru, ne znam, ali trazi u tom pravcu.

edit: wuick google mi je dao ovo:
.data # Tells assembler we're in the data segment
val: .word 10, -14, 30 # Three words placed in memory


Pozdrav Branimire,

pa stavio sam u writable data sekciju var1 .space20 koja zauzima 20 bajta, i probao da stavim memorijsku lokaciju od var1 kao drugi argument

Code:

    la    a1,0(var1)


ali dobijam Program received signal SIGSEGV, Segmentation fault. stvarno ne znam šta da stavim i kako kao drugi argument
Signaure Hamer Dev .inc
 
Odgovor na temu

Branimir Maksimovic

Član broj: 64947
Poruke: 5534
c-bg-d-p1-76.bvcom.net.



+1064 Profil

icon Re: Read and print int u assembly (MIPS) programu29.03.2017. u 14:38 - pre 85 meseci
Ah pa sta ide posle syscall? Moras da pozoves sys_exit.
 
Odgovor na temu

vidonk
Slobodan Vidovic
Niksic

Član broj: 192156
Poruke: 354
*.crnagora.net.



+9 Profil

icon Re: Read and print int u assembly (MIPS) programu29.03.2017. u 21:48 - pre 85 meseci
Riješio sam evo ga cio program, imao sam neke "zaostale" naredbe koje su prouzrokovale segm. fault

Code:
/*
 * hello-1.2/Makefile
 *
 * This file is subject to the terms and conditions of the GNU General Public
 * License.  See the file "COPYING" in the main directory of this archive
 * for more details.
 *
 * Copyright (C) 1995, 1997 by Ralf Baechle
 */
#include <regdef.h>
#include <sys/asm.h>
#include <sys/syscall.h>

EXPORT(__start)


    .set    noreorder
    LEAF(main)

#Prompt for the integer to enter
    li    a0,1        # first argument fd 1 for iutput
    la    a1,prompt    # second argument memory location of hello string
    li    a2,20        # lenght of string to print
    li    v0,__NR_write    # syscall write,they are defined in unistd.h
    syscall

#Read the integer and save it in s0
    nop
    nop
    li    a0,0        # firs argument, for stdin is 0 ISTR, see "man 2 read"
    la    a1,(var1)    # second argument load adress of var1 into a1
    li    a2,12     # third argument is count of byts 
    li    v0,__NR_read
    syscall
    move t0, v0


#Print the message
    li    a0,1        # first argument fd 1
    la    a1,output    # second argument memory location of hello string
    li    a2,256        # lenght of string to print
    li    v0,__NR_write    # syscall write,they are defined in unistd.h
    syscall

#Output the number
    nop
    nop


quit:            # label quit
    li    v0,__NR_exit    # load system call for exit in v0
    syscall

    nop

    END(main)

    .data

prompt:    .asciiz    "Enter an intriger: "
output:    .asciiz    "\nYou typed the number "
var1:       .space     256

Signaure Hamer Dev .inc
 
Odgovor na temu

[es] :: Asembler :: Read and print int u assembly (MIPS) programu

[ Pregleda: 1929 | Odgovora: 4 ] > FB > Twit

Postavi temu Odgovori

Navigacija
Lista poslednjih: 16, 32, 64, 128 poruka.