Roger S
Posts: 9
Joined: Sun Jan 07, 2018 10:05 pm

Problem calling ncurses

Mon Nov 26, 2018 7:58 pm

I am having problems calling ncurses from Assembly code. The code I am calling is
/* ccalls.c */
#include <ncurses.h>

char inchar()
{
int ch;
initscr();
cbreak();
ch =getch();
endwin();
return (ch);
}
And I am calling it by
key_c:
bl inchar @ Call c
stmea DSTK!, {r0} @ Put on data stack
b NEXT
Debugging the code with gdb, I can follow it for hundreds of instructions into the lcurses library code but I loose track of what the code is doing before it fails with the message:- dl-lookup.c: No such file or derectory.
I can call it in c and it works correctly
/* ctest */
#include<stdio.h>
char inchar();

int main7()
{
char ch:
ch = inchar();
printf(" Char was %c "), ch );
return(0);
{
It seems that "inchar" is called correctly and starts running but when called from assembler is unable to find the requisite files despite gcc not complaining about missing files.

Regards Roger

User avatar
Paeryn
Posts: 2986
Joined: Wed Nov 23, 2011 1:10 am
Location: Sheffield, England

Re: Problem calling ncurses

Tue Nov 27, 2018 3:37 am

You'll need to provide a bit more information like your compiling / linking command lines, and if you can a small enough example that is failing. I just ran a quick test with what you gave, expanded the assembly code slightly to be working code and assuming using the standard C startup (though I've just tested without C startup running it as _start rather than main and it still works).

dl-lookup.c is part of the dynamic loader, it sounds like it is failing to find a required library at run-time but that is strange if it works fine in your C code.

Code: Select all

	.file "asmtest.s"
	.section .rodata
	.align 2
Text:
	.ascii "Character was %c\012\000"
	.text
	.align 2
	.global main
	.syntax unified
	.arm
	.type main, %function
main:
	push {lr}
	bl inchar
	mov r1, r0
	ldr r0, =Text
	bl printf
	pop {pc}
Compiling and running...

Code: Select all

pi@rpi3:~/Programming/asm/ncur $ gcc asmtest.s ccalls.o -lncurses -o asmtest
pi@rpi3:~/Programming/asm/ncur $ ./asmtest
Character was d
pi@rpi3:~/Programming/asm/ncur $
She who travels light — forgot something.

jahboater
Posts: 5825
Joined: Wed Feb 04, 2015 6:38 pm
Location: West Dorset

Re: Problem calling ncurses

Tue Nov 27, 2018 8:36 am

The command "strace" might help the OP see whats going on.

strace ./armtest

Roger S
Posts: 9
Joined: Sun Jan 07, 2018 10:05 pm

Re: Problem calling ncurses

Wed Nov 28, 2018 12:15 am

Thank you for your reply. I tried using your "asmtest.s" in my code and found that it sometimes worked. I eventually found that initialising my stack pointers was causing it to fail. I am working on a implementation of FORTH so I need more than one stack. I have reworked the initialisation and it seems to be working, at least enough to make progress although I have yet to find out why it did not work.


Regards Roger

User avatar
Paeryn
Posts: 2986
Joined: Wed Nov 23, 2011 1:10 am
Location: Sheffield, England

Re: Problem calling ncurses

Wed Nov 28, 2018 1:13 am

Initialising your stacks shouldn't affect calling foreign code so long as you follow the AAPCS which defines which registers are used for what, which need preserving and which may be trashed across foreign function calls.

It also imposes how the stack is, especially alignment. Just thought this might trip you up if you only align the stack to words, it has to be double words i.e. 8-byte alignment for 32bit code. If you have it misaligned when calling foreign functions then they may fail as they will assume 8-byte.
She who travels light — forgot something.

Return to “C/C++”