Jump to content

Can someone assemble some code for me?

- - - - -

  • Please log in to reply
5 replies to this topic

#1
Charlemagne

Charlemagne

    Newbie

  • Members
  • Pip
  • 2 posts
Please, I just need to know if I fulfill the assignment, I cant get this compiled in my machine or on a vm, if you have to fiddle a little that fine, but i need to know if my body code fulfills the assignment. This is midterm practice. Ive tried and tried and just cant get it to assemble on my own, my teacher is terrible and cant tell us how to.

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


#2
RhetoricalRuvim

RhetoricalRuvim

    JavaScript Programmer

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,252 posts
  • Location:C:\Countries\US
By

Quote

mov edx:eax, eax
, did you mean
mov [edx+eax], eax 
?

#3
Charlemagne

Charlemagne

    Newbie

  • Members
  • Pip
  • 2 posts

RhetoricalRuvim said:

By , did you mean
mov [edx+eax], eax 
?



No, I mean mov the value in eax into the 64bit register edx:eax

#4
Gunner

Gunner

    Learning Programmer

  • Members
  • PipPipPip
  • 45 posts

Charlemagne said:

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?

Apparently NOT to help someone with an attitude like yours!

mov edx:eax, eax

that is not how you move a value into a 64 bit register... edx and eax are not 64bit they are 32 bit.. and that is not the proper syntax...

mov    64bitreg, eax
64bit regs start with r.. rax. rsi etc...

#5
RhetoricalRuvim

RhetoricalRuvim

    JavaScript Programmer

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,252 posts
  • Location:C:\Countries\US
And don't the operands have to be the same size?

#6
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,705 posts
  • Programming Language:C, Java, C++, PHP, Python, Perl, Assembly, Bash, Others
  • Learning:JavaScript
@RR: Yes.
@Charlemagne:

You can't do this:

mov     edx:eax, eax


So just do this:

xor     edx, edx


sudo rm -rf /




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users