Advertisement
Guest User

estudo-assembly

a guest
Sep 26th, 2017
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.42 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. void Exercicio1();
  6. // Somar o conteúdo de um array
  7. void Exercicio2();
  8. // Dados dos números A e B, se A > B então Variável C = 1, caso contrário C = 0;
  9. void Exercicio3();
  10. // Dados os termos da equação de bascará: A,B e C calcule o Delta
  11. void Exercicio4();
  12. // Dada uma tabela chamada Tabela1 com 10 elementos quaisquer grave na variável R maior valor da tabela.
  13. void Exercicio5();
  14. // Descobrir tamanho de uma string
  15. void Exercicio6();
  16. // Dada duas variáveis strings cujo a primeira chama se STR1 e esta com o valor “FACENS” cujo o tamanho é 6 caracteres.Faça a transferência de dados de STR1 para STR2.
  17. void Exercicio7();
  18. // Dado duas strings STR1 e STR2 faça a comparação das mesmas, se STR1 = STR2 então Variável R = 1 caso contrário R = 0
  19. void Exercicio8();
  20. // Data uma tabela chamada TabelaSort com 10 elementos, efetua a ordenação da mesma.(utilize o algoritmo bolha)
  21. void Exercicio9();
  22. // Dada uma string de tamanho qualquer, invertê-la
  23. void Exercicio10();
  24. // Criar um vetor com tamanho T, encontrar quantas vezes um número desejado D aparece no vetor;
  25. void Exercicio11();
  26. // Descobrir maior valor entre 3 variáveis
  27. void Exercicio12();
  28. // Contar as vogais em uma string
  29. void Exercicio13();
  30. // Fatorial
  31. void Exercicio14();
  32. // Testando Pilha
  33. void Exercicio15();
  34.  
  35. int main() {
  36.  
  37.     Exercicio1();
  38.     Exercicio2();
  39.     Exercicio3();
  40.     Exercicio4();
  41.     Exercicio5();
  42.     Exercicio6();
  43.     Exercicio7();
  44.     Exercicio8();
  45.     Exercicio9();
  46.     //Exercicio10(); Com erro
  47.     Exercicio11();
  48.     Exercicio12();
  49.     Exercicio13();
  50.     Exercicio14();
  51.     Exercicio15();
  52.  
  53.     getchar();
  54.  
  55.     return 0;
  56. }
  57.  
  58. void Exercicio15()
  59. {
  60.     cout << "Exercicio 15" << endl;
  61.  
  62.     int a = 5, b = 4;
  63.     int ultimo;
  64.  
  65.     __asm {
  66.         push a
  67.         push b
  68.         pop eax
  69.         pop eax
  70.         mov ultimo, eax
  71.     }
  72.  
  73.     cout << ultimo << endl;
  74. }
  75.  
  76. void Exercicio14(){
  77.  
  78.     cout << "Exercicio 14" << endl;
  79.  
  80.     short total;
  81.  
  82.     __asm {
  83.         mov bx, 6
  84.             call  proc_fact
  85.             mov total, ax
  86.             jmp fim
  87.  
  88.         proc_fact :
  89.             cmp   bl, 1
  90.             jg    do_calculation
  91.             mov   ax, 1
  92.             ret
  93.  
  94.         do_calculation :
  95.         dec   bl
  96.             call  proc_fact
  97.             inc   bl
  98.             mul   bl; ax = al * bl
  99.             ret
  100.  
  101.         fim:
  102.     }
  103.  
  104.     cout << total << endl;
  105.  
  106. }
  107. void Exercicio13()
  108. {
  109.     cout << "Exercicio 13" << endl;
  110.     int total;
  111.     char s[8] = "PALAVRA";
  112.     int tam = 7;
  113.  
  114.     __asm {
  115.         lea eax, s
  116.             xor ebx, ebx
  117.             xor ecx, ecx
  118.  
  119.         looping :
  120.         cmp ebx, 7
  121.             je fim
  122.             jmp compA
  123.         proximo :
  124.         add eax, 1
  125.             add ebx, 1
  126.             jmp looping
  127.  
  128.         compA :
  129.         cmp[eax], 'A'
  130.             je incrementa
  131.             cmp[eax], 'a'
  132.             je incrementa
  133.             jmp compE
  134.  
  135.         compE :
  136.         cmp[eax], 'E'
  137.             je incrementa
  138.             cmp[eax], 'e'
  139.             je incrementa
  140.             jmp compI
  141.  
  142.         compI :
  143.         cmp[eax], 'I'
  144.             je incrementa
  145.             cmp[eax], 'i'
  146.             je incrementa
  147.             jmp compO
  148.  
  149.         compO :
  150.         cmp[eax], 'O'
  151.             je incrementa
  152.             cmp[eax], 'o'
  153.             je incrementa
  154.             jmp compU
  155.  
  156.         compU :
  157.         cmp[eax], 'U'
  158.             je incrementa
  159.             cmp[eax], 'u'
  160.             je incrementa
  161.             jmp proximo
  162.  
  163.         incrementa :
  164.         add ecx, 1
  165.             mov total, ecx
  166.             jmp proximo
  167.  
  168.         fim :
  169.     }
  170.  
  171.     cout << total << endl;
  172. }
  173.  
  174. void Exercicio12()
  175. {
  176.     cout << "Exercicio 12" << endl;
  177.  
  178.     int a = 3, b = -4, c = 1, maior;
  179.  
  180.     __asm {
  181.         mov eax, a
  182.             mov maior, eax
  183.  
  184.         compB :
  185.         cmp eax, b
  186.             jge compC
  187.             mov eax, b
  188.             mov maior, eax
  189.  
  190.         compC :
  191.         cmp eax, c
  192.             jge fim
  193.             mov eax, c
  194.             mov maior, eax
  195.         fim :
  196.     }
  197.  
  198.     cout << maior << endl;
  199. }
  200.  
  201. void Exercicio11()
  202. {
  203.     cout << "Exercicio 11" << endl;
  204.  
  205.     int v[29] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
  206.     int qtde, D = 1;
  207.  
  208.     __asm
  209.     {
  210.         xor eax, eax
  211.             mov qtde, eax
  212.             lea eax, v
  213.             xor bx, bx
  214.             xor edx, edx
  215.  
  216.         looping :
  217.         cmp bx, 28
  218.             je fim
  219.             mov ecx, [eax]
  220.             cmp ecx, D
  221.             je aumenta
  222.         incremento :
  223.         add eax, 4
  224.             inc bx
  225.             jmp looping
  226.  
  227.         aumenta :
  228.         inc edx
  229.             jmp incremento
  230.  
  231.         fim :
  232.         mov qtde, edx
  233.     }
  234.  
  235.     cout << qtde << endl;
  236. }
  237.  
  238. void Exercicio10()
  239. {
  240.     cout << "Exercicio 10" << endl;
  241.  
  242.     int tam;
  243.  
  244.     char s[11] = "mensagem 1";
  245.     char s2[11] = "          ";
  246.  
  247.     __asm
  248.     {
  249.         lea eax, s
  250.             xor ebx, ebx
  251.             xor ecx, ecx
  252.             mov tam, ecx
  253.  
  254.         looping :
  255.         cmp[eax], '\0'
  256.             je fim
  257.             add ecx, 1
  258.             add eax, 1
  259.             jmp looping
  260.  
  261.         fim :
  262.         mov tam, ecx
  263.  
  264.             ; achado o tamanho da string, é hora de învertê - la
  265.  
  266.             lea eax, s
  267.             lea ebx, s2
  268.             mov ecx, tam
  269.             add ebx, ecx
  270.             xor ecx, ecx
  271.  
  272.         looping2 :
  273.         cmp ecx, tam - 1
  274.             je fim2
  275.             mov edx, [eax]
  276.             mov[ebx], edx
  277.             add eax, 1
  278.             add ebx, -1
  279.             add ecx, 1
  280.             jmp looping2
  281.  
  282.         fim2 :
  283.     }
  284.  
  285.     cout << s2 << endl;
  286. }
  287.  
  288. void Exercicio9()
  289. {
  290.     cout << "Exercicio 9" << endl;
  291.     int v[] = { 9, 10, 8, 7, 6, 5, 4, 3, 2, 1 };
  292.     int aux, i, flag = 1, tam = 10;
  293.  
  294.     __asm
  295.     {
  296.     inicio:
  297.         lea eax, v
  298.             mov ebx, flag
  299.             cmp ebx, 1
  300.             jne fim
  301.             mov ebx, 0
  302.             mov flag, ebx
  303.  
  304.             mov ecx, 0
  305.         innerFor:
  306.         mov i, ecx
  307.             mov edx, tam
  308.             add edx, -1
  309.             cmp ecx, edx
  310.             jge inicio
  311.             mov edx, [eax + 4]
  312.             cmp edx, [eax]
  313.             jge incremento; Mudar aqui para trocar entre crescente / decrescente
  314.             mov ecx, [eax]
  315.             mov[eax], edx
  316.             mov[eax + 4], ecx
  317.             mov ebx, 1
  318.             mov flag, ebx
  319.         incremento :
  320.         mov ecx, i
  321.             inc ecx
  322.             add eax, 4
  323.             jmp innerFor
  324.  
  325.         fim :
  326.     }
  327.  
  328.     for (int i = 0; i < 10; i++)
  329.     {
  330.         cout << v[i] << " ";
  331.     }
  332.     cout << endl;
  333. }
  334.  
  335. void Exercicio8()
  336. {
  337.     cout << "Exercicio 8" << endl;
  338.  
  339.     char s[7] = "FACENS";
  340.     char s2[7] = "FACENS";
  341.     int R;
  342.  
  343.     __asm
  344.     {
  345.         lea eax, s
  346.             lea ebx, s2
  347.             mov ecx, 1
  348.             mov R, ecx
  349.  
  350.         looping :
  351.         cmp[eax], '\0'
  352.             je fim
  353.             mov ecx, [eax]
  354.             cmp ecx, [ebx]
  355.             jne naoiguais
  356.             add eax, 1
  357.             add ebx, 1
  358.             jmp looping
  359.  
  360.         naoiguais :
  361.         mov ecx, 0
  362.             mov R, ecx
  363.             jmp fim
  364.  
  365.         fim :
  366.     }
  367.  
  368.     cout << R << endl;
  369. }
  370.  
  371. void Exercicio7()
  372. {
  373.     cout << "Exercicio 7" << endl;
  374.  
  375.     char s[7] = "FACENS";
  376.     char s2[7] = "      ";
  377.  
  378.     __asm
  379.     {
  380.         lea eax, s
  381.             lea ebx, s2
  382.  
  383.         looping :
  384.         cmp[eax], '\0'
  385.             je fim
  386.             mov ecx, [eax]
  387.             mov[ebx], ecx
  388.             add ebx, 1
  389.             add eax, 1
  390.             jmp looping
  391.  
  392.         fim :
  393.     }
  394.  
  395.     cout << s2 << endl;
  396. }
  397.  
  398. void Exercicio6()
  399. {
  400.     cout << "Exercicio 6" << endl;
  401.  
  402.     int tam;
  403.  
  404.     char s[] = "FACENSSSSSSSSSSS";
  405.  
  406.     __asm
  407.     {
  408.         lea eax, s
  409.             xor ebx, ebx
  410.             xor ecx, ecx
  411.             mov tam, ecx
  412.  
  413.         looping :
  414.         cmp[eax], '\0'
  415.             je fim
  416.             add ecx, 1
  417.             add eax, 1
  418.             jmp looping
  419.  
  420.         fim :
  421.         mov tam, ecx
  422.     }
  423.  
  424.     cout << tam << endl;
  425. }
  426.  
  427. void Exercicio5()
  428. {
  429.     cout << "Exercicio 5" << endl;
  430.  
  431.     int v[] = { 10, 2, 3, 4, -1, 6, 7, 8, 9, 10 };
  432.     int R, menor;
  433.  
  434.     __asm
  435.     {
  436.         lea eax, v
  437.             xor cx, cx
  438.             mov edx, [eax]
  439.             mov menor, edx
  440.             add eax, 4
  441.  
  442.         looping:
  443.         cmp cx, 9
  444.             je fim
  445.             mov edx, menor
  446.             mov ebx, [eax]
  447.             cmp[eax], edx
  448.             jl troca
  449.             jmp proximo
  450.         troca :
  451.         mov edx, [eax]
  452.             mov menor, edx
  453.         proximo :
  454.         inc cx
  455.             add eax, 4
  456.             jmp looping
  457.  
  458.         fim :
  459.         mov eax, menor
  460.             mov R, eax
  461.     }
  462.  
  463.     cout << R << endl;
  464. }
  465.  
  466. void Exercicio4()
  467. {
  468.     cout << "Exercicio 4" << endl;
  469.     int A, B, C, delta, aux;
  470.     A = 1;
  471.     B = 4;
  472.     C = 3;
  473.  
  474.     __asm
  475.     {
  476.         // Resultado do mul fica no EAX
  477.         mov eax, B
  478.             mul eax
  479.             mov delta, eax
  480.  
  481.             mov eax, -4
  482.             imul A
  483.             imul C
  484.  
  485.             mov ebx, delta
  486.             add eax, ebx
  487.             mov delta, eax
  488.     }
  489.  
  490.     cout << delta << endl;
  491. }
  492.  
  493. void Exercicio3()
  494. {
  495.     cout << "Exercicio 3" << endl;
  496.     int A, B, C;
  497.     A = 3;
  498.     B = 2;
  499.  
  500.     __asm
  501.     {
  502.         mov eax, A
  503.             mov ebx, B
  504.             mov ecx, C
  505.             cmp eax, ebx
  506.             jg cum
  507.             jmp czero
  508.  
  509.         cum :
  510.         mov ecx, 1
  511.             mov C, ecx
  512.             jmp fim
  513.         czero :
  514.         mov ecx, 0
  515.             mov C, ecx
  516.  
  517.         fim :
  518.     }
  519.  
  520.     cout << C << endl;
  521.  
  522. }
  523.  
  524. void Exercicio2()
  525. {
  526.     cout << "Exercicio 2" << endl;
  527.     int vet[] = { 1, 5, 2, 6, 4, 1, 5, 2, 6, 4 }, s, aux;
  528.  
  529.     __asm
  530.     {
  531.         mov eax, 0
  532.             mov s, eax
  533.             xor cx, cx
  534.             lea eax, vet
  535.  
  536.         looping :
  537.         cmp cx, 10
  538.             jge fim
  539.             mov edx, [eax]
  540.             mov aux, edx
  541.             mov edx, s
  542.             add edx, aux
  543.             mov s, edx
  544.             add eax, 4
  545.             inc cx
  546.             jmp looping
  547.  
  548.         fim :
  549.     }
  550.  
  551.     cout << s << endl;
  552. }
  553.  
  554. void Exercicio1()
  555. {
  556.     cout << "Exercicio 1" << endl;
  557.     /*int *a = new int(), *b = new int();
  558.     *a = 5;
  559.     *b = 10;*/
  560.     int a = 5, b = 10;
  561.  
  562.     __asm
  563.     {
  564.         mov eax, a
  565.             mov ecx, b
  566.             push eax
  567.             push ecx
  568.             call troca
  569.             pop b
  570.             pop a
  571.             jmp fim
  572.  
  573.         troca :
  574.         push ebp
  575.             mov ebp, esp
  576.             push ebx
  577.             push esi
  578.             push edi
  579.  
  580.             mov eax, [ebp + 12]
  581.             mov ecx, [ebp + 8]
  582.             mov[ebp + 12], ecx
  583.             mov[ebp + 8], eax
  584.  
  585.             pop edi
  586.             pop esi
  587.             pop ebx
  588.             pop ebp
  589.             ret
  590.         fim :
  591.  
  592.     }
  593.  
  594.     printf("%d %d\n", a, b);
  595.  
  596. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement