Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Lista simplesmente encadeada
linkedList.o: formato do arquivo elf64-x86-64
Desmontagem da seção .text:
0000000000000000 <CriaElemento>:
0: 55 push rbp
1: 48 89 e5 mov rbp,rsp
4: 48 83 ec 10 sub rsp,0x10
8: bf 20 00 00 00 mov edi,0x20
d: e8 00 00 00 00 call 12 <CriaElemento+0x12>
12: 48 89 45 f8 mov QWORD PTR [rbp-0x8],rax
16: 48 8b 45 f8 mov rax,QWORD PTR [rbp-0x8]
1a: 48 8b 55 10 mov rdx,QWORD PTR [rbp+0x10]
1e: 48 89 50 08 mov QWORD PTR [rax+0x8],rdx
22: 48 8b 55 18 mov rdx,QWORD PTR [rbp+0x18]
26: 48 89 50 10 mov QWORD PTR [rax+0x10],rdx
2a: 48 8b 55 20 mov rdx,QWORD PTR [rbp+0x20]
2e: 48 89 50 18 mov QWORD PTR [rax+0x18],rdx
32: 48 8b 45 f8 mov rax,QWORD PTR [rbp-0x8]
36: 48 c7 00 00 00 00 00 mov QWORD PTR [rax],0x0
3d: 48 8b 45 f8 mov rax,QWORD PTR [rbp-0x8]
41: c9 leave
42: c3 ret
0000000000000043 <ObterValorPessoa>:
43: 55 push rbp
44: 48 89 e5 mov rbp,rsp
47: 48 89 7d c8 mov QWORD PTR [rbp-0x38],rdi
4b: 48 89 75 c0 mov QWORD PTR [rbp-0x40],rsi
4f: 89 55 bc mov DWORD PTR [rbp-0x44],edx
52: c7 45 d4 00 00 00 00 mov DWORD PTR [rbp-0x2c],0x0
59: 48 8b 45 c0 mov rax,QWORD PTR [rbp-0x40]
5d: 48 8b 00 mov rax,QWORD PTR [rax]
60: 48 89 45 d8 mov QWORD PTR [rbp-0x28],rax
64: eb 50 jmp b6 <ObterValorPessoa+0x73>
66: 8b 45 d4 mov eax,DWORD PTR [rbp-0x2c]
69: 3b 45 bc cmp eax,DWORD PTR [rbp-0x44]
6c: 75 39 jne a7 <ObterValorPessoa+0x64>
6e: 48 8b 45 d8 mov rax,QWORD PTR [rbp-0x28]
72: 48 8b 50 08 mov rdx,QWORD PTR [rax+0x8]
76: 48 89 55 e0 mov QWORD PTR [rbp-0x20],rdx
7a: 48 8b 50 10 mov rdx,QWORD PTR [rax+0x10]
7e: 48 89 55 e8 mov QWORD PTR [rbp-0x18],rdx
82: 48 8b 40 18 mov rax,QWORD PTR [rax+0x18]
86: 48 89 45 f0 mov QWORD PTR [rbp-0x10],rax
8a: 48 8b 45 c8 mov rax,QWORD PTR [rbp-0x38]
8e: 48 8b 55 e0 mov rdx,QWORD PTR [rbp-0x20]
92: 48 89 10 mov QWORD PTR [rax],rdx
95: 48 8b 55 e8 mov rdx,QWORD PTR [rbp-0x18]
99: 48 89 50 08 mov QWORD PTR [rax+0x8],rdx
9d: 48 8b 55 f0 mov rdx,QWORD PTR [rbp-0x10]
a1: 48 89 50 10 mov QWORD PTR [rax+0x10],rdx
a5: eb 31 jmp d8 <ObterValorPessoa+0x95>
a7: 83 45 d4 01 add DWORD PTR [rbp-0x2c],0x1
ab: 48 8b 45 d8 mov rax,QWORD PTR [rbp-0x28]
af: 48 8b 00 mov rax,QWORD PTR [rax]
b2: 48 89 45 d8 mov QWORD PTR [rbp-0x28],rax
b6: 48 83 7d d8 00 cmp QWORD PTR [rbp-0x28],0x0
bb: 75 a9 jne 66 <ObterValorPessoa+0x23>
bd: 48 8b 45 c8 mov rax,QWORD PTR [rbp-0x38]
c1: 48 8b 55 e0 mov rdx,QWORD PTR [rbp-0x20]
c5: 48 89 10 mov QWORD PTR [rax],rdx
c8: 48 8b 55 e8 mov rdx,QWORD PTR [rbp-0x18]
cc: 48 89 50 08 mov QWORD PTR [rax+0x8],rdx
d0: 48 8b 55 f0 mov rdx,QWORD PTR [rbp-0x10]
d4: 48 89 50 10 mov QWORD PTR [rax+0x10],rdx
d8: 48 8b 45 c8 mov rax,QWORD PTR [rbp-0x38]
dc: 5d pop rbp
dd: c3 ret
00000000000000de <Adiciona>:
de: 55 push rbp
df: 48 89 e5 mov rbp,rsp
e2: 48 83 ec 40 sub rsp,0x40
e6: 48 89 7d d8 mov QWORD PTR [rbp-0x28],rdi
ea: 48 8b 45 10 mov rax,QWORD PTR [rbp+0x10]
ee: 48 89 04 24 mov QWORD PTR [rsp],rax
f2: 48 8b 45 18 mov rax,QWORD PTR [rbp+0x18]
f6: 48 89 44 24 08 mov QWORD PTR [rsp+0x8],rax
fb: 48 8b 45 20 mov rax,QWORD PTR [rbp+0x20]
ff: 48 89 44 24 10 mov QWORD PTR [rsp+0x10],rax
104: e8 00 00 00 00 call 109 <Adiciona+0x2b>
109: 48 89 45 f0 mov QWORD PTR [rbp-0x10],rax
10d: 48 8b 45 d8 mov rax,QWORD PTR [rbp-0x28]
111: 48 8b 00 mov rax,QWORD PTR [rax]
114: 48 85 c0 test rax,rax
117: 75 0d jne 126 <Adiciona+0x48>
119: 48 8b 45 d8 mov rax,QWORD PTR [rbp-0x28]
11d: 48 8b 55 f0 mov rdx,QWORD PTR [rbp-0x10]
121: 48 89 10 mov QWORD PTR [rax],rdx
124: eb 64 jmp 18a <Adiciona+0xac>
126: 48 8b 45 d8 mov rax,QWORD PTR [rbp-0x28]
12a: 48 8b 00 mov rax,QWORD PTR [rax]
12d: 48 89 45 e8 mov QWORD PTR [rbp-0x18],rax
131: eb 0b jmp 13e <Adiciona+0x60>
133: 48 8b 45 e8 mov rax,QWORD PTR [rbp-0x18]
137: 48 8b 00 mov rax,QWORD PTR [rax]
13a: 48 89 45 e8 mov QWORD PTR [rbp-0x18],rax
13e: 48 8b 45 e8 mov rax,QWORD PTR [rbp-0x18]
142: 48 8b 00 mov rax,QWORD PTR [rax]
145: 48 85 c0 test rax,rax
148: 75 e9 jne 133 <Adiciona+0x55>
14a: bf 20 00 00 00 mov edi,0x20
14f: e8 00 00 00 00 call 154 <Adiciona+0x76>
154: 48 89 45 f8 mov QWORD PTR [rbp-0x8],rax
158: 48 8b 45 f8 mov rax,QWORD PTR [rbp-0x8]
15c: 48 8b 55 10 mov rdx,QWORD PTR [rbp+0x10]
160: 48 89 50 08 mov QWORD PTR [rax+0x8],rdx
164: 48 8b 55 18 mov rdx,QWORD PTR [rbp+0x18]
168: 48 89 50 10 mov QWORD PTR [rax+0x10],rdx
16c: 48 8b 55 20 mov rdx,QWORD PTR [rbp+0x20]
170: 48 89 50 18 mov QWORD PTR [rax+0x18],rdx
174: 48 8b 45 f8 mov rax,QWORD PTR [rbp-0x8]
178: 48 c7 00 00 00 00 00 mov QWORD PTR [rax],0x0
17f: 48 8b 45 e8 mov rax,QWORD PTR [rbp-0x18]
183: 48 8b 55 f8 mov rdx,QWORD PTR [rbp-0x8]
187: 48 89 10 mov QWORD PTR [rax],rdx
18a: c9 leave
18b: c3 ret
000000000000018c <MostrarTodosElementos>:
18c: 55 push rbp
18d: 48 89 e5 mov rbp,rsp
190: 48 83 ec 20 sub rsp,0x20
194: 48 89 7d e8 mov QWORD PTR [rbp-0x18],rdi
198: 48 8b 45 e8 mov rax,QWORD PTR [rbp-0x18]
19c: 48 8b 00 mov rax,QWORD PTR [rax]
19f: 48 89 45 f8 mov QWORD PTR [rbp-0x8],rax
1a3: eb 32 jmp 1d7 <MostrarTodosElementos+0x4b>
1a5: 48 8b 45 f8 mov rax,QWORD PTR [rbp-0x8]
1a9: 8b 48 18 mov ecx,DWORD PTR [rax+0x18]
1ac: 48 8b 45 f8 mov rax,QWORD PTR [rbp-0x8]
1b0: 48 8b 50 10 mov rdx,QWORD PTR [rax+0x10]
1b4: 48 8b 45 f8 mov rax,QWORD PTR [rbp-0x8]
1b8: 8b 40 08 mov eax,DWORD PTR [rax+0x8]
1bb: 89 c6 mov esi,eax
1bd: bf 00 00 00 00 mov edi,0x0
1c2: b8 00 00 00 00 mov eax,0x0
1c7: e8 00 00 00 00 call 1cc <MostrarTodosElementos+0x40>
1cc: 48 8b 45 f8 mov rax,QWORD PTR [rbp-0x8]
1d0: 48 8b 00 mov rax,QWORD PTR [rax]
1d3: 48 89 45 f8 mov QWORD PTR [rbp-0x8],rax
1d7: 48 83 7d f8 00 cmp QWORD PTR [rbp-0x8],0x0
1dc: 75 c7 jne 1a5 <MostrarTodosElementos+0x19>
1de: c9 leave
1df: c3 ret
00000000000001e0 <ObterQuantidadeElemento>:
1e0: 55 push rbp
1e1: 48 89 e5 mov rbp,rsp
1e4: 48 89 7d e8 mov QWORD PTR [rbp-0x18],rdi
1e8: 48 8b 45 e8 mov rax,QWORD PTR [rbp-0x18]
1ec: 48 8b 00 mov rax,QWORD PTR [rax]
1ef: 48 89 45 f8 mov QWORD PTR [rbp-0x8],rax
1f3: c7 45 f4 00 00 00 00 mov DWORD PTR [rbp-0xc],0x0
1fa: eb 0f jmp 20b <ObterQuantidadeElemento+0x2b>
1fc: 83 45 f4 01 add DWORD PTR [rbp-0xc],0x1
200: 48 8b 45 f8 mov rax,QWORD PTR [rbp-0x8]
204: 48 8b 00 mov rax,QWORD PTR [rax]
207: 48 89 45 f8 mov QWORD PTR [rbp-0x8],rax
20b: 48 83 7d f8 00 cmp QWORD PTR [rbp-0x8],0x0
210: 75 ea jne 1fc <ObterQuantidadeElemento+0x1c>
212: 8b 45 f4 mov eax,DWORD PTR [rbp-0xc]
215: 5d pop rbp
216: c3 ret
0000000000000217 <main>:
217: 55 push rbp
218: 48 89 e5 mov rbp,rsp
21b: 48 83 c4 80 add rsp,0xffffffffffffff80
21f: 48 c7 45 b0 00 00 00 mov QWORD PTR [rbp-0x50],0x0
226: 00
227: c7 45 c0 01 00 00 00 mov DWORD PTR [rbp-0x40],0x1
22e: 48 c7 45 c8 00 00 00 mov QWORD PTR [rbp-0x38],0x0
235: 00
236: c7 45 d0 14 00 00 00 mov DWORD PTR [rbp-0x30],0x14
23d: 48 8d 45 b0 lea rax,[rbp-0x50]
241: 48 8b 55 c0 mov rdx,QWORD PTR [rbp-0x40]
245: 48 89 14 24 mov QWORD PTR [rsp],rdx
249: 48 8b 55 c8 mov rdx,QWORD PTR [rbp-0x38]
24d: 48 89 54 24 08 mov QWORD PTR [rsp+0x8],rdx
252: 48 8b 55 d0 mov rdx,QWORD PTR [rbp-0x30]
256: 48 89 54 24 10 mov QWORD PTR [rsp+0x10],rdx
25b: 48 89 c7 mov rdi,rax
25e: e8 00 00 00 00 call 263 <main+0x4c>
263: 48 8d 45 b0 lea rax,[rbp-0x50]
267: 48 89 c7 mov rdi,rax
26a: e8 00 00 00 00 call 26f <main+0x58>
26f: bf 0a 00 00 00 mov edi,0xa
274: e8 00 00 00 00 call 279 <main+0x62>
279: 48 8d 45 e0 lea rax,[rbp-0x20]
27d: 48 8d 4d b0 lea rcx,[rbp-0x50]
281: ba 00 00 00 00 mov edx,0x0
286: 48 89 ce mov rsi,rcx
289: 48 89 c7 mov rdi,rax
28c: e8 00 00 00 00 call 291 <main+0x7a>
291: 48 8d 45 b0 lea rax,[rbp-0x50]
295: 48 89 c7 mov rdi,rax
298: e8 00 00 00 00 call 29d <main+0x86>
29d: 89 45 ac mov DWORD PTR [rbp-0x54],eax
2a0: 8b 45 e0 mov eax,DWORD PTR [rbp-0x20]
2a3: 89 c6 mov esi,eax
2a5: bf 00 00 00 00 mov edi,0x0
2aa: b8 00 00 00 00 mov eax,0x0
2af: e8 00 00 00 00 call 2b4 <main+0x9d>
2b4: bf 0a 00 00 00 mov edi,0xa
2b9: e8 00 00 00 00 call 2be <main+0xa7>
2be: 8b 45 ac mov eax,DWORD PTR [rbp-0x54]
2c1: 89 c6 mov esi,eax
2c3: bf 00 00 00 00 mov edi,0x0
2c8: b8 00 00 00 00 mov eax,0x0
2cd: e8 00 00 00 00 call 2d2 <main+0xbb>
2d2: b8 00 00 00 00 mov eax,0x0
2d7: c9 leave
2d8: c3 ret
/**
* Fonte do algoritmo:
* http://codereview.stackexchange.com/questions/110165/simple-singly-linked-list-implementation-in-c
*
* Comando para produzir o Assembly mais legível:
* gcc -o linkedList -g -c linkedList.c
* objdump -d -M intel -S linkedList.o > allout.txt 2>&1
* Fonte: http://stackoverflow.com/a/1289907/5429980
**/
#include <stdio.h>
#include <stdlib.h>
struct Pessoa
{
int id;
char *nome;
int idade;
};
struct Elemento
{
struct Elemento *proximoElemento;
struct Pessoa pessoa;
};
struct ListaEncadeada
{
struct Elemento *primeiroElemento;
};
struct Elemento* CriaElemento(struct Pessoa pessoa)
{
struct Elemento *novoElemento;
novoElemento = malloc(sizeof(struct Elemento));
novoElemento->pessoa = pessoa;
novoElemento->proximoElemento = NULL;
return novoElemento;
}
struct Pessoa ObterValorPessoa(struct ListaEncadeada *lista, int indice)
{
int iterateIndice = 0;
struct Pessoa pessoaRetorno;
struct Elemento *elementoTemporario = lista->primeiroElemento;
while(elementoTemporario != NULL)
{
if (iterateIndice == indice)
{
pessoaRetorno = elementoTemporario->pessoa;
return pessoaRetorno;
}
++iterateIndice;
elementoTemporario = elementoTemporario->proximoElemento;
}
return pessoaRetorno;
}
void Adiciona(struct ListaEncadeada *lista, struct Pessoa pessoa)
{
struct Elemento *novoElemento = CriaElemento(pessoa);
if (lista->primeiroElemento == NULL)
{
lista->primeiroElemento = novoElemento;
}
else
{
struct Elemento *ultimoElemento;
ultimoElemento = lista->primeiroElemento;
while (ultimoElemento->proximoElemento != NULL)
{
ultimoElemento = ultimoElemento->proximoElemento;
}
struct Elemento *novoElemento;
novoElemento = malloc(sizeof(struct Elemento));
novoElemento->pessoa = pessoa;
novoElemento->proximoElemento = NULL;
ultimoElemento->proximoElemento = novoElemento;
}
}
void MostrarTodosElementos(struct ListaEncadeada *lista)
{
struct Elemento *ultimoElemento = lista->primeiroElemento;
while(ultimoElemento != NULL)
{
printf("id: %d, nome: %s, idade: %d\n", ultimoElemento->pessoa.id, ultimoElemento->pessoa.nome, ultimoElemento->pessoa.idade);
ultimoElemento = ultimoElemento->proximoElemento;
}
}
int ObterQuantidadeElemento(struct ListaEncadeada *lista)
{
struct Elemento *elementoTemporario = lista->primeiroElemento;
int tamanho = 0;
while (elementoTemporario != NULL)
{
++tamanho;
elementoTemporario = elementoTemporario->proximoElemento;
}
return tamanho;
}
int main(void)
{
/**Implementação**/
struct ListaEncadeada lista = { NULL };
struct Pessoa pessoaNova;
pessoaNova.id = 1;
pessoaNova.nome = "Dener";
pessoaNova.idade = 20;
Adiciona(&lista, pessoaNova);
MostrarTodosElementos(&lista);
printf("\n");
struct Pessoa pessoaObtida = ObterValorPessoa(&lista, 0);
int quantidadeElementos = ObterQuantidadeElemento(&lista);
printf("\nId da pessoa obtida pelo indice: %d", pessoaObtida.id);
printf("\n");
printf("\nQuantidade de elementos: %d\n", quantidadeElementos);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.