Other Programming Languages
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming Languages - MoreOther Programming Languages

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
SlickEdit: Code in over 40 languages across 7 platforms. SlickEdit’s unmatched power, speed, and flexibility allows even the most accomplished developers to write better code faster. Download a free trial today!
  #1  
Old January 11th, 2006, 08:01 PM
the axiom the axiom is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2006
Posts: 2 the axiom User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 6 m 18 sec
Reputation Power: 0
Need help with assembly language question

Hello

I need help with an A level computing exam that is coming up very soon.

Sometimes in the exam students are asked to write some assembly language code. A typical question is:

Using the trace table below write the assembly language equivalent of the following:

X=0
While Not (x = 999)
Do x = x + 1
End While

(In the exam the trace table contains the columns “label”, “opcode” and “operand”)

And the answer would be:

LDA #0
Loop CMP #999
BEQ Label1
ADD #1
JMP Loop
Label1 STA X

(the above code is filled in the relevant columns)

However last year the exam asked a question that seems different from all the previous years:

Using the trace table below write the assembly language equivalent of the following:

Count = 0
Repeat
x = x + x
count = count + 1
until x >= 999

you can assume that the program counter will contain the address of location “start” to execute your code.

(then the trace table is partially filled in and the students need to fill in the rest)

Code:
Label	Opcode	Operand	comment
x		15	Location x contains value 15
count			This location is reserved to store the value of count
start


The model answer provided by the exam board was


Start: LDA #0
STA Count Store zero in Count
Loop LDA X Load value from location X into accumulator
ADD X Add contents of X to accumulator
STA X Save contents of accumulator at location labelled X
INC Count Increment value in Count
CMP #999 Compare contents of accumulator with 999
BLT Loop Conditional branch if contents of accumulator is < 999


Now I understand most of the code except two things:

1, the line “INC count” why isn’t count loaded into a register and then incremented and then after the algorithm is finished it could be stored in the memory location? Is it possible to add to a memory location without loading their value into a register? E.g. ADD memlocation1,#1

2, if it is possible to add to memory locations without having to load their contents into a register then why is value of X loaded into the accumulator and then incremented and the stored back into the memory location for every set of the iteration? Wouldn’t it be better to write STA X just once at the end of the code?

I would really appreciate it if someone could help me with this. I need to understand the model answer given by the exam board so that similar answers can be given for future questions.

Reply With Quote
  #2  
Old January 12th, 2006, 10:58 AM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 5th Plane (7000 - 7499 posts)
 
Join Date: Nov 2001
Location: Glendale, Los Angeles County, California, USA
Posts: 7,394 Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level) 
Time spent in forums: 4 Weeks 1 Day 8 h 36 m 18 sec
Reputation Power: 715
Quote:
Originally Posted by the axiom
1, the line “INC count” why isn’t count loaded into a register and then incremented and then after the algorithm is finished it could be stored in the memory location? Is it possible to add to a memory location without loading their value into a register? E.g. ADD memlocation1,#1

This is very dependent on the CPU model. Most RISC chips don't support any instruction like this, but quite a few CISC chips do. The x86 family has add to memory instructions, for instance. RISC chips usually have only two instructions that deal with memory (LDA and STA).

Quote:
Originally Posted by the axiom
2, if it is possible to add to memory locations without having to load their contents into a register then why is value of X loaded into the accumulator and then incremented and the stored back into the memory location for every set of the iteration? Wouldn’t it be better to write STA X just once at the end of the code?

Again this is very dependent on the CPU model. Maybe this particular CPU supports INC and DEC instructions directly to a memory location, but not an ADD instruction. In x86 CPU family, they do support ADD to memory instructions, but not all CPUs do this.
__________________
Up the Irons
What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
"Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
Down with Sharon Osbourne

Puzzle of the Month solved by sizeablegrin, etienne141 and L7Sqr, superior C/C++ programmers of the month

Reply With Quote
  #3  
Old January 12th, 2006, 01:15 PM
the axiom the axiom is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2006
Posts: 2 the axiom User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 6 m 18 sec
Reputation Power: 0
thank you very much for that reply. i did more research on that processor and it helped a lot. thanks.

Reply With Quote
  #4  
Old March 30th, 2007, 01:42 AM
aman19886 aman19886 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2007
Posts: 3 aman19886 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 25 m 37 sec
Reputation Power: 0
Need help to convert a 32 bit mapping into 16 bit

Hi I have a 32 bit mapping code of my friend but I wanna make my code for 16 bit its like a boy moving its arms i hv found made the bit map for that but dont know how to program it if you would like I can send you the 32 bit mapping code if you can help me. I will really appriciate it.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreOther Programming Languages > Need help with assembly language question


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway