Skip to content

Instantly share code, notes, and snippets.

@nmf2
Last active December 5, 2017 16:00
Show Gist options
  • Select an option

  • Save nmf2/9916e1b679f8fc0b7c2e97a652251236 to your computer and use it in GitHub Desktop.

Select an option

Save nmf2/9916e1b679f8fc0b7c2e97a652251236 to your computer and use it in GitHub Desktop.
Resolução IHS 2015 Q2
; code.asm
SECTION .data
vol: dq 0
txt: db 10,"vol: %f", 10, 0
SECTION .text
global cone
extern printf
cone:
enter 0, 0
finit
mov ebx, [ebp + 8] ; *vol do cone
fldpi
fld dword[ebp + 12] ; empilha o raio
fmul st0,st0 ; calcula e empilha: raio^2
fmul st0,st1 ; calcula e empilha: PI * raio^2
fld dword[ebp + 16] ; empilha altura
fmul st0,st1 ; calcula e empilha: altura * PI * raio^2
mov eax, 3
push eax
fild dword[esp] ; empilha 3
pop eax
fdivp st1, st0 ; calcula e empilha: altura * PI * raio^2 / 3
fst dword[ebx] ; armazena na memoria
;fst dword[vol] ; armazena na memoria
leave
mov eax, 0
ret
//code.c
#include <stdio.h>
extern void cone (float *, float, float);
int main (){
float r, a, vol;
printf("Enter the radius and the height: \n");
scanf("%f %f", &r, &a);
cone(&vol, r, a);
printf("vol: %f\n", vol);
return 0;
}
// nasm -f elf code2.asm && gcc -m32 code2.c code2.o && ./a.out
section .data
temp: dq 0.0
addr: dq 0
section .text
global calculaSomaPG
calculaSomaPG:
enter 0, 0
finit
mov eax, dword[ebp + 8]; eax = *sn
fld dword[ebp + 12]; st0 = a1
fld dword[ebp + 16]; st0 = q, st1 = a1
fld dword[ebp + 16]; st0 = q, st1 = q, st1 = a1
mov ecx, [ebp + 20]; ecx = n
cmp ecx, 1
jle end_qn;
dec ecx
qn: ;calcula q^n
fmul st0, st1 ; st0 = st0 * q
loop qn ; ecx--
end_qn:
;FPU st0 = q^n, st1 = q, st1 = a1
fld1 ; st0 = 1, st1 = q^n, st2 = 1, st3 = a1
fsubp st1, st0 ; st0 = q^n -1, st1 = q, st2 = a1
fmul st0, st2 ; st0 = (q^n -1) * a1, st1 = q, st2 = a1
fstp dword[temp]; st0 = q, st1 = a1, [temp] = (q^n -1) * a1
fld1 ; st0 = 1, st1 = q, st2 = a1
fsubp st1, st0 ; st0 = q - 1, st1 = a1
fld dword[temp] ; st0 = a1* (q^n -1) st1 = (q - 1) , st2 = a1
fdiv st0, st1 ; st0 = a1* (q^n -1)/(q - 1) , st1 = a1
fstp dword[eax] ; [eax] = &sn = a1 * (q^n -1)/(q - 1), st0 = a1
faddp st0, st0 ; st0 = null, simply pop st0
leave
mov eax, 0
re
#include <stdio.h>
#include <math.h>
extern void calculaSomaPG(float *, float, float, int);
int main(void){
float sn = 0.0, a1, q;
int n;
scanf("%f %f %d", &a1, &q, &n);
calculaSomaPG( &sn, a1, q, n );
printf("correct: %f\n", a1*(pow(q, n) - 1)/(q - 1));
printf("asm: %f\n", sn);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment