wtf, I have 50 views and not a single reply? What is the point of this forum?
is no one willing to just plug this code in?
This is my homework:
(1) Write an assembly program that asks for three positive integers, and then asks for a guess as to the average of those numbers (the nearest integer). Print whether the guess integer was the nearest integer value to the average. Do this without use of any branches (see example in Sec. 3.3).
(2) Write an assembly program that asks for two positive integers, evaluate their product using shifts, not the MUL instruction (see Ch. 3 Intro).
(3) Write a (Big/Little) Endian Detector for a given hardware system (see Fig. 3.4)
This is my code:
avg3int.asm
%include "asm_io.inc" segment .data prompt1 db "Enter a positive interger: ", 0 prompt2 db "Enter a second positive interger: ", 0 prompt3 db "Enter a third positive interger: ", 0 prompt4 db "Enter a guess of their average: ", 0 outmsg1 db "You entered ", 0 outmsg2 db ", and ", 0 outmsg3 db "You guessed that the average is ", 0 outmsg4 db "You did you guess correctly? (0 = no, 1 = yes)", 0 outmsg5 db "The average of the numbers is ", 0 segment .bss input1 resd 1 input2 resd 1 input3 resd 1 guess resd 1 ; The guess number is stored here segment .text global asm_main asm_main: enter 0,0 pusha mov eax, prompt1 call print_string call read_int mov [input1], eax mov eax, prompt2 call print_string call read_int mov [input2], eax mov eax, prompt3 call print_string call read_int mov [input3],eax mov eax, prompt4 call print_string call read_int mov [guess], eax mov eax, [input1] add eax, [input2] add eax, [input3] mov edx:eax, eax mov ebx, 3 div ebx ; divides edx:eax by 3 and stores the quotient in eax and remainder in edx mov ecx, eax ; frees up eax by putting the quotient in ecx mov eax, outmsg1 call print_string ; prints "You entered " mov eax, [input1] call print_int ; prints input1 mov eax, outmsg2 call print_string ; prints ", and " mov eax, [input2] call print_int ; prints input2 mov eax, outmsg2 call print_string ; prints ", and " mov eax, [input3] call print_int ; prints input3 mov eax, outmsg3 call print_string ; prints "You guessed that the average is " move eax, outmsg4 call print_string ; prints "You did you guess correctly? (0 = no, 1 = yes)" xor ebx, ebx cmp ecx, [guess] sete bl neg ebx mov edx, ebx and edx, ecx not ebx and ebx, [guess] or edx, ebx mov eax, outmsg5 call print_string ; prints "The average of the numbers is " mov eax, ecx call print_int ; prints the average call print_nl ; prints a new line popa mov eax, 0 leave ret mul2int.asm %include "asm_io.inc" segment .data prompt1 db "Enter a positive interger: ", 0 prompt2 db "Enter another positive interger: ", 0 outmsg1 db "You entered ", 0 outmsg2 db ", and ", 0 outmsg3 db ", and their product is ", 0 segment .bss input1 resd 1 ; the number to be multiplied input2 resd 1 ; the number to multply by segment .text global asm_main asm_main: enter 0,0 pusha mov eax, prompt1 call print_string call read_int mov [input1], eax mov eax, prompt2 call print_string call read_int mov [input2], eax mov eax, [input1] ; input 1 is in eax mov ebx, [input2] ; input 2 is in ebx xor ecx, ecx ; the product is in ecx xor edx, edx ; 0 is in edx cmp edx, eax ; compare input 1 to 0 jle invalid_input ; if input 1 is <= 0, return 0 as product cmp edx, ebx ; compare input 2 to 0 jle invalid_input ; if input 2 is <= 0, return 0 as product loop: add ecx, ebx ; add input 2 to the product sal ebx, 1 sar eax, 1 cmp eax, edx ; compare eax to 0 jzn loop ; if eax is not 0, loop mov eax, outmsg1 call print_string ; prints "You entered " mov eax, [input1] call print_int ; prints input1 mov eax, outmsg2 call print_string ; prints ", and " mov eax, [input2] call print_int ; prints input2 mov eax, outmsg3 call print_string ; prints ", and their product is " invalid_input: mov eax, ecx call print_int ; prints the product call print_nl ; prints a new line popa mov eax, 0 leave ret endianness.asm %include "asm_io.inc" segment .data outmsg1 db "Big Endian Machine", 0 outmsg2 db "Little Endian Machine", 0 segment .bss segment .text global asm_main asm_main: enter 0,0 pusha mov ax, 15 cmp ah, 0 jnz label1; mov eax, outmsg1 call print_string ; prints "Big Endian Machine" label1: mov eax, outmsg2 call print_string ; prints " Little Endian Machine" call print_nl ; prints a new line popa mov eax, 0 leave ret
Edited by dargueta, 19 March 2011 - 01:02 AM.
Added code tags


Sign In
Create Account

Back to top









