Very simple assembly introduction code.
Seems to compile ok through gcc -o prog1 prog1.s
, then ./prog1
just skips a line and shows nothing, like waiting an input the code doesn't ask. What's wrong?
Using gcc (Debian 4.7.2-5) 4.7.2 in 64-bit gNewSense running on VMware.
Code:
/*
int nums[] = {10, -21, -30, 45};
int main() {
int i, *p;
for (i = 0, p = nums; i != 4; i++, p++)
printf("%d\n", *p);
return 0;
}
*/
.data
nums: .int 10, -21, -30, 45
Sf: .string "%d\n" # string de formato para printf
.text
.globl main
main:
/********************************************************/
/* mantenha este trecho aqui e nao mexa - prologo !!! */
pushq %rbp
movq %rsp, %rbp
subq $16, %rsp
movq %rbx, -8(%rbp)
movq %r12, -16(%rbp)
/********************************************************/
movl $0, %ebx /* ebx = 0; */
movq $nums, %r12 /* r12 = &nums */
L1:
cmpl $4, %ebx /* if (ebx == 4) ? */
je L2 /* goto L2 */
movl (%r12), %eax /* eax = *r12 */
/*************************************************************/
/* este trecho imprime o valor de %eax (estraga %eax) */
movq $Sf, %rdi /* primeiro parametro (ponteiro)*/
movl %eax, %esi /* segundo parametro (inteiro) */
call printf /* chama a funcao da biblioteca */
/*************************************************************/
addl $1, %ebx /* ebx += 1; */
addq $4, %r12 /* r12 += 4; */
jmp L1 /* goto L1; */
L2:
/***************************************************************/
/* mantenha este trecho aqui e nao mexa - finalizacao!!!! */
movq $0, %rax /* rax = 0 (valor de retorno) */
movq -8(%rbp), %rbx
movq -16(%rbp), %r12
leave
ret
/***************************************************************/
%al
beforecall printf
as you don't use any SSE registers for arguments. Still, that is unlikely to cause this problem. You could try running the program throughstrace
or of course use a debugger. – Tobitobiahgcc -Wall -g prog1.s
,gdb a.out
,layout next
,run
+ ^C:0x00007ffff7a9e1d0 <printf+64> jmpq *%rax
highlighted. In regular terminal:Program received signal SIGINT, Interrupt. 0x00007ffff7a9e1d0 in printf () from /lib/x86_64-linux-gnu/libc.so.6
Now what? – Drierp/a $rax
? If that points back to itself for whatever reason, then it would be an endless loop. – Tobitobiahp/a
but%rax
is where the '0' return value of themain
function is stored. If$rax
refers to the memory address associated to it I SUPPOSE it's the mentioned above. Btw ran other assembly code slightly different and it's all good with the new one. – Drierjmpq
do ap/a $rax
to see the value. – Tobitobiah0x00007ffff7a9e1d0
in printf () from /lib/x86_64-linux-gnu/libc.so.6 (gdb) p/a $raxp/a $rax $1 = 0x7ffff7a9e1ca <printf+58>
– Drier%al
. Do that and it works. Full answer and explanation coming shortly. – Freeway